[英]Is there any way to override code written loggers from json configuration in Serilog
I have this code written:我写了这段代码:
return Host.CreateDefaultBuilder()
.UseSerilog((context, services, configuration) => configuration
.MinimumLevel.Verbose()
.ReadFrom.Configuration(_configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.Enrich.With(UserLoginEnricher.GetInstance())
.Enrich.With(new LoggersAdditionalEnricher())
//CSV logger
.WriteTo.Logger(x =>
{
x.WriteTo.File(ApplicationConfiguration.FullPathToCsvFile,
rollingInterval: RollingInterval.Day,
encoding: Encoding.Unicode,
retainedFileCountLimit: ApplicationConfiguration.AppSettingsOptions.RetainDataCount,
outputTemplate: "{Message}{NewLine}");
//log filter
x.Filter.ByIncludingOnly(e =>
{
if (!e.Properties.ContainsKey(nameof(LogType)))
return false;
return e.Properties[nameof(LogType)].ToString() == LogType.CSV.ToString();
});
})
.WriteTo(x =>{...})
.WriteTo(x =>{...});
Is there any way to configure json SERILOG configuration to change minimum level of File logger without code changes?有什么方法可以配置 json SERILOG 配置以更改文件记录器的最低级别而无需更改代码?
My JSON config of serilog:我的 Serilog 的 JSON 配置:
"Serilog": {
"Using": [
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Warning",
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
}
}
}
I have more loggers.我有更多的记录器。 I need all loggers to write with Warning level and this CSV logger to write on Information level
我需要所有记录器以警告级别写入,并且此 CSV 记录器以信息级别写入
You can use this nuget package for doing configuration in appsettings.json for Serilog.您可以使用此 nuget package 在 appsettings 中进行配置。json 用于 Serilog。 https://github.com/serilog/serilog-settings-configuration
https://github.com/serilog/serilog-settings-configuration
and there great extension which even helps us to convert code to proper configuration.并且有很好的扩展,甚至可以帮助我们将代码转换为正确的配置。 https://marketplace.visualstudio.com/items?itemName=Suchiman.SerilogAnalyzer
https://marketplace.visualstudio.com/items?itemName=Suchiman.SerilogAnalyzer
and to restrict log level for a particular sink you can specify like this并限制特定接收器的日志级别,您可以像这样指定
"WriteTo": [
{
"Name": "File",
"Args": { "restrictedToMinimumLevel": "Information" }
}
]
you need to add other arguments as per your requirement.您需要根据您的要求添加其他 arguments。
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.