Skip to content

Using appsettings to configure #5

Closed
@nrandell

Description

@nrandell

Not really an issue, but I've just experimented with using appsettings to configure the logger. I don't know if this is something worth adding in or not, so I'll just include a few code snippets.

Happy to create a PR if it's valuable.

First off, in the extensions

public static IWebHostBuilder UseSerilogLogger(this IWebHostBuilder builder)
{
    if (builder == null) throw new ArgumentNullException(nameof(builder));
    builder.ConfigureServices((context, collection) =>
        collection.AddSingleton<ILoggerFactory>(new LoggerFactory(context.Configuration))
    );
    return builder;
}

Then in the factory

public LoggerFactory(IConfiguration configuration)
{
    Log.Logger = new LoggerConfiguration()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();
    _provider = new SerilogLoggerProvider();
}

And finally my settings

  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console"
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions