Whenever a civil action is commenced against any person holding the office of county treasurer, county auditor, or any other officer, for performing or attempting to perform any duty authorized or directed by any statute of this state for the collection of the public revenue, such treasurer, auditor or other officer may, in the discretion of the court before whom such action is brought, by an order made by such court and entered in the minutes thereof, be allowed and paid out of the county treasury, reasonable fees of counsel and other expenses for defending such action.