[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? 🙂

Connecting to SharePoint in Azure Functions using App Only Permissions

So, recently I needed to create a service that would read and write some data to SharePoint Online, easy enough, isn’t it? Problem was that the scenario demanded that the processing were done in Azure, specifically in Azure Functions. Hmmm… How difficult can it be? (Well, it has to do with SharePoint, so…)

The first thing I did was to import the usual packages that I use to connect to SharePoint:

Both packages had worked perfectly in all previous scenarios, then I had no reason to not try it.

Problem was that the version that the runtime downloaded didn’t have an option for obtaining an App Only Authorization Token, which was necessary in my case.

So, cutting to the chase, after some struggle I found a version of TokenHelper.cs that had the methods that I needed and then I’ve been able to successfully convert it to .csx. The file is available in the following link:

TokenHelper.csx

This file will allow you to connect to SharePoint Online using App-Only Authorization like this:

This is as simple as it gets 😀

I hope this helps anyone who is starting with Azure Functions and SharePoint!