Integrating Serilog with Azure Functions and Application Insights

...

Serilog traditionally doesn't have support for Azure Functions, however there is an awesome Sink Project that I now maintain that allows to add Application Insights integration with Serilog. It's mostly self explanatory, however it's platform agnostic and can be used from any project type like console application or asp.net web api etc.

In order to use it with Azure Functions you can simply add this piece of code in Azure Function static constructor (or a global static initialisation function, if you have many function files):

static MyFunctions()
{
    var config = TelemetryConfiguration.Active;
    if (config != null)
    {
        //logging
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .Enrich.FromLogContext()
            .WriteTo.ApplicationInsightsTraces(config)
            .CreateLogger();
    }
}

Note that I'm using TelemetryConfiguration.Active here, which makes serilog _pick up already configured _Application Insights. Also, in order for this to work, you need to enable AI integration by following the official documentation.

Once done, you will be able to not only see those messages in AI, but log messages will also be correlated between functions calls. What this means is Functions Portal will be able to know which messages are related to which function executions!


Thanks for reading. If you would like to follow up with future posts please subscribe to our rss feed.