Search code examples
asp.net-core.net-coreasp.net-core-logging

Options and Configuration for console logging - how does it work in source code?


I'm trying to implement my own custom logger. I've been looking at the implementation of the Console Logger of microsoft.

You can configure the ConsoleLoggerOptions in AppSettings like this:

{
  "Logging": {
    "IncludeScopes": true,
    "LogLevel": {
      "Default": "Information",
      "System": "Warning",
      "Microsoft": "Warning",
    },
    "Console": {
      "DisableColors": true
    }
}

Looking at AddConsole() extension: https://github.com/dotnet/extensions/blob/master/src/Logging/Logging.Console/src/ConsoleLoggerFactoryExtensions.cs#L18

How does the internal works for the Options? Where does it "bind" it with 'Console', where does it go from ConsoleLoggerOptions to Console?

I just don't understand after this point. Just adding the Options to services just 'works'?


Solution

  • So i dug a bit deeper:

    ConsoleLoggerProvider has a provider alias which renames it to "Console": https://github.com/dotnet/extensions/blob/master/src/Logging/Logging.Console/src/ConsoleLoggerProvider.cs#L14

    And the configuration gets added based on the alias name: https://github.com/dotnet/extensions/blob/55518d79834d3319c91f40b449d028338b129ed6/src/Logging/Logging.Configuration/src/LoggerProviderConfigurationFactory.cs#L27

    pretty well hidden imo