serilog-sinks-mysql icon indicating copy to clipboard operation
serilog-sinks-mysql copied to clipboard

Pull Configuration from appsettings.json

Open mrUlrik opened this issue 5 years ago • 1 comments

I am pulling Serilog into the application via WebHostBuilder as documented here: https://github.com/serilog/serilog-aspnetcore#inline-initialization

This does not appear to load the connection string or the table name from the appsettings.json file, however it is using this file to pick up the Using: Serilog.Sinks.MySQL line.

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.MySQL" ], 
    "LevelSwitches": { "$controlSwitch": "Verbose" },
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Debug"
      },
      {
        "Name": "MySQL",
        "Args": {
          "connectionString": "Serilog",
          "tableName": "serilog",
          "storeTimestampInUtc": "false"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  },
  "ConnectionStrings": {
    "Serilog": "Server=<my server>;Database=<my db>;Uid=<my user>;Pwd=<mypassword>;"
  }
}

If I specify the connection string manually by adding the following line to the code documented in the link above, everything works normally:

.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
  .ReadFrom.Configuration(hostingContext.Configuration)
  .WriteTo.MySQL(hostingContext.Configuration.GetConnectionString("Serilog")))

Do I have the configuration incorrect in the settings file?

mrUlrik avatar Apr 24 '19 01:04 mrUlrik

Does work for you? Thanks

skoruba avatar Dec 02 '19 20:12 skoruba