code_blue
code_blue

Reputation: 571

Does Serilog captures log automatically when configured with Log Level

I am using Serilog for the first-time and trying to figure how it works. I am looking at a sample ASP.Net Core application where Serilog is configured. The serilog configuration is being read from Appsettings.json:

"Serilog": {
    "ApplicationName": "app-name",
    "Using": [ "Serilog.Sinks.File" ],
    "MinimumLevel": "Verbose",
    "WriteTo": [
        {
            "Name": "RollingFile",
            "Args": { "pathFormat": "log-{Date}.log" }
        }
    ]
}

The Log object is defined in Startup and "AddSerilog" is defined in "ConfigureServices" Method. But other than that no other log information is present. However when I run the app automatic log files are being generated in the Solution directory.

Question is does Serilog automatically creates those log files?

Upvotes: 0

Views: 1161

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93273

The call to AddSerilog in ConfigureServices registers a logging provider with Microsoft's ILoggingBuilder. This provider (SerilogLoggerProvider), implements ILoggerProvider and its CreateLogger method, which looks like this:

public ILogger CreateLogger(string name)
{
    return new SerilogLogger(this, _logger, name);
}

It's clear from this code that Serilog's SerilogLogger implements Microsoft's ILogger interface.

When the ASP.NET Core framework needs to create a logger object, it uses implementations of ILoggerProvider to create instances of ILogger by calling CreateLogger. With the registered Serilog provider in place, this ends up with ASP.NET Core asking for an instance of ILogger and receiving an instance of SerilogLogger, which provides a bridge between ASP.NET Core and Serilog.

The RollingFile sink itself creates the configured log file automatically whenever a log event is emitted, with its own logic for creating timestamped files, etc, accordingly.

Upvotes: 2

Related Questions