Search code examples
c#asp.netasp.net-coreserilogappsettings

Serilog appsettings config with custom format provider


I'm trying to configure Serilog in appsettings.json, and I have a custom format provider I want to use. I'm having issues getting Serilog to find my class.

My project is an ASP.NET Core project using .NET 7.

Originally I configured Serilog in code like this in Program.cs:

using TestProj.Formatting;

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.Console(formatProvider: new Iso8601Formatter())
    .CreateLogger();

Iso8601Formatter is a class in TestProj.Formatting. Using this approach everything works fine, it compiles and output is formatted like expected.

Trying to move the config into appsettings.json the relevant section looks like this:

"Serilog": {
    "Using": ["TestProj"],
    "WriteTo": [
        {
            "Name": "Console",
            "Args": {
                "formatProvider": "TestProj.Formatting.Iso8601Formatter"
            }
        }
    ]
}

When trying to start up the project I get the following error: Unhandled exception. System.InvalidOperationException: Type TestProj.Formatting.Iso8601Formatter was not found.

I've double checked that the TestProj.dll is copied to the output folder (TestProj\bin\Debug\net7.0), and it's there. How can I get Serilog to find my custom format provider?


Solution

  • You have to pass the name of the assembly.

    "Serilog": {
        "Using": ["TestProj"],
        "WriteTo": [
            {
                "Name": "Console",
                "Args": {
                    "formatProvider": "TestProj.Formatting.Iso8601Formatter, Testproj"
                }
            }
        ]
    }
    

    'Testproj' is the full name of the assembly.