问题
I found a post where the blogger explained how to filter by LogEvent level to a separate file for Serilog configuration. I am doing all my Serilog configuration in my appsettings.json. How would this look in json configuration, I can't seem to figure how to json the lambda expression....
Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning).WriteTo.RollingFile(@"Logs\Warning-{Date}.log"))
I am using configuration for Serilog from my appsettings.json and am trying to convert this
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning).WriteTo.RollingFile(@"Logs\ApplicationName\Serilog\Warning-{Date}.log"))
to json, to include in my Serilog section of my appsettings file
EDIT: appsettings partial shown here
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}"
}
},
{
"Name": "Seq",
"Args": { "serverUrl": "http://localhost:5341" }
},
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "RollingFile",
"Args": { "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/log-{Date}.log" }
}
]
}
}
],
"SubLogger": {
"Level": "Warnings",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Warnings/log-{Date}.log"
},
The sub logger pathFormat is not producing the same folder naming as the RollingFile pathFormat
回答1:
For this moment Serilog does not support configuration of sub-loggers through JSON appsettings. See this issue on github.
It's not an easy task actually because you pass Func<LogEvent, bool>
to ByIncludingOnly()
filter. Mapping configuration data from json file to c# code is not a trivial task.
However if you are just interested in creation of sub-logger for specific log level, you could combine configuration from JSON config with ByIncludingOnly()
filter.
Define a POCO that will hold filter configuration:
public class SubLoggerConfiguration
{
public LogEventLevel Level { get; set; }
private string pathFormat;
public string PathFormat
{
get => pathFormat;
set => pathFormat = value.Replace("%APPLICATION_NAME%", Environment.GetEnvironmentVariable("APPLICATION_NAME"));
}
}
Add SubLogger
section to your JSON config:
{
"Serilog": {
"Using": [
"Serilog.Sinks.RollingFile"
],
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "RollingFile",
"Args": { "pathFormat": "c:\\Logs\\log-{Date}.log" }
}
],
"SubLogger": {
"Level": "Warning",
"pathFormat": "Logs\\ApplicationName\\Serilog\\Warning-{Date}.log"
}
}
}
It's a good idea to keep it inside native Serilog section, it will not break configuration of Serilog itself.
Then load SubLogger
configuration from config file:
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("AppSettings.json");
IConfiguration configuration = configurationBuilder.Build();
SubLoggerConfiguration subLoggerConfiguration = new SubLoggerConfiguration();
configuration.GetSection("Serilog:SubLogger").Bind(subLoggerConfiguration);
Note that you have to install Microsoft.Extensions.Configuration.Binder
NuGet package for binding configuration to a POCO.
Now subLoggerConfiguration will contain desired log level and path format for the log. You can use this settings to call ByIncludingOnly()
filter:
Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == subLoggerConfiguration.Level).WriteTo.RollingFile(subLoggerConfiguration.PathFormat));
来源:https://stackoverflow.com/questions/47565008/trouble-converting-serilog-configuration-code-line-to-json-configuration