简体   繁体   中英

Binding an appsettings.json section to a list of POCO

Given these two classes

public class SerilogSubLoggerConfigurations
{
    public List<SerilogSubLoggerConfiguration> SerilogConfigurations { get; set; }
}

public class SerilogSubLoggerConfiguration
{
    private string _pathFormat;

    public LogEventLevel Level { get; set; }

    public string PathFormat
    {
        get => _pathFormat;
        set => _pathFormat = value.Replace("%APPLICATION_NAME%", Environment.GetEnvironmentVariable("APPLICATION_NAME"));
    }
}

and an apssettings.json section that looks like this

 "Serilog": {
    "SubLogger": [
  {
    "Level": "Information",
    "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Information/log-{Date}.log"
  },
  {
    "Level": "Warning",
    "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Warning/log-{Date}.log"
  },
  {
    "Level": "Critical",
    "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
  }
],
 }

I want to bind the SubLogger section to the SerilogSubLoggerConfigurations class. I cannot seem to figure out how to get this configuration to create that class with the three items (of type SerilogSubLoggerConfiguration ).

In Json config, array of your sublogger objects is called SubLogger but in configuration POCO SerilogSubLoggerConfigurations it's called SerilogConfigurations . That's why configuration binder could not match them.

In ASP.NET Core there is no possibility (at least for this moment) to change the name of mapped properties, they should match the names of configuration sections. Check this answer for details.

So you should just rename either POCO property or JSON section name. In your case SubLoggers seems to be the most appropriate name:

public class SerilogSubLoggerConfigurations
{
    public List<SerilogSubLoggerConfiguration> SubLoggers { get; set; }
}

{
  "Serilog": {
    "SubLoggers": [
      {
        "Level": "Information",
        "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Information/log-{Date}.log"
      },
      {
        "Level": "Warning",
        "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Warning/log-{Date}.log"
      },
      {
        "Level": "Critical",
        "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
      }
    ]
  }
}

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("AppSettings.json");
IConfiguration configuration = configurationBuilder.Build();
SerilogSubLoggerConfigurations subLoggerConfigurations = configuration.GetSection("Serilog").Get<SerilogSubLoggerConfigurations>();

One more note. Serilog.Events.LogEventLevel enum does not have a Critical level, it has Error and Fatal . That's why this item

{
  "Level": "Critical",
  "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
}

will not be loaded correctly. You should change it to:

{
  "Level": "Error",
  "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Error/log-{Date}.log"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM