user3344711
user3344711

Reputation: 31

Setting up Serilog for AzureFunction

I'm relatively new to C# and Azure and this thing confuses me to no end. What I want is to log everything Information+ to Seq, and to override MS/System to Warning+.

Startup.cs

using System;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;

[assembly: FunctionsStartup(typeof(SillyApp.Startup))]

namespace SillyApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                .MinimumLevel.Override("System", LogEventLevel.Warning)
                .WriteTo.Seq("https://blahblah.bla")
                .CreateLogger();

            builder.Services.AddLogging(b => { b.AddSerilog(logger); });
        }
    }
}

My understanding is that I've now added a Serilog-provider to the logging Providers. This works in that Seq receives, but the overrides does nothing. The Minimumlevel.Information does work however.

SillyApp.cs

using System;
using System.Text;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace SillyApp
{
    public class MyClass
    {
        private ILogger<MyClass> _logger;

        public MyClass(ILogger<MyClass> log)
        {
            _logger = log;
        }

        [FunctionName("TimedThing")]
        public async Task RunAsync([TimerTrigger("*/10 * * * * *")] TimerInfo timer)
        {
            _logger.LogInformation("Helloes");
        }
    }
}

My theory is that logging providers now include both the default one and the Serilog one. And somehow they combine and make my life a misery. What shows up in Seq is a whole bunch of stuff. Executed messages, "1 function loaded", "Host Initialized/started". Tagged as Information, but doesn't even show in my Run Window.

So if my theory is correct, how do I override the default logging provider? If I'm mistaken then can someone please tell me what's going on?

Upvotes: 3

Views: 753

Answers (1)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31797

Not all Serilog features work with AddSerilog(), minimum level overrides are one of those.

In regular apps, UseSerilog() (the newer API) on IHostBuilder will work correctly with all Serilog features.

Unfortunately, I don't think Azure Function apps give you an IHostBuilder to work with.

The better alternative will be to use Seq.Extensions.Logging (dotnet add package Seq.Extensions.Logging) instead of Serilog, and to write to Seq using:

        services.AddLogging(b => b.AddSeq("https://blahblah.bla"));

You will then be able to use Microsoft.Extensions.Logging level overrides in your appsettings.json configuration file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    },
    "Seq": {
      "ServerUrl": "https://blahblah.bla",
      "ApiKey": "1234567890",
      "LogLevel": {
        "System": "Information",
        "Microsoft": "Warning"
      }
    }
  }
}

Upvotes: 1

Related Questions