[Azure Functions] Function wide Log Service

I confess, I am log-addicted. If I have to guess I would say that at least 1/3 of my code are log calls. It despairs me to look at a console and don’t know exactly what is going on.

That said, Azure Functions provides a very simple-to-use system to log to the execution console. You just have to add a TraceWriter object as a parameter for the Run method.

Couldn’t be easier, I think. But when your function is a little more complex than that you will probably have some separated code files for your business logic (I hope, for the sake of you sanity) and that is when it becomes a little complicated to log your processing.

The obvious choice is to pass the TraceWriter object around through all your methods and simply use it.

It really can be a viable solution if we are talking about two or three methods, but it will be a real mess if you have more than this.

So, I came up with a solution that worked really well for me 😀
The solutions consists of initializing a global object at the beginning of the Function that will be used throughout the code, just importing the Service code file.

The Service code file can be found here. It is really a just simple piece of code. To use it, you just have to instanciate the object at the beginning of the function Execution:

The Log object in line 6 is global and static and is defined in the LogService.csx file. The LogService class is also defined in the same file.

And then you can use it anywhere in the code, just referencing the Service code file

How does it look? 🙂