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

668 views Asked by At

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'?

1

There are 1 answers

0
Marcel On BEST ANSWER

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