英文:
How to configure Serilog (in the json file) to log info to file, but verbose to console?
问题
以下是翻译好的部分:
这是我的配置:
"Serilog": {
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"restrictedToMinimumLevel": "Information"
},
{
"Name": "File",
"Args": {
"path": "../logs/log-.txt",
"rollingInterval": "Day"
}
}
]
}
这是C#代码:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
Log.Information("Starting up ...");
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext());
...
}
...
我的目标是能够在不重新编译代码的情况下调整配置文件,以更改日志级别。
上述配置会将 Debug 日志同时发送到控制台和文件,尽管我试图仅将 Info 日志发送到控制台。但不起作用 - 两个输出都显示 Debug。
我使用的版本是 Serilog.AspNetCore 6.1.0
我错过了什么?
英文:
Here is my configuration:
"Serilog": {
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"restrictedToMinimumLevel": "Information"
},
{
"Name": "File",
"Args": {
"path": "../logs/log-.txt",
"rollingInterval": "Day"
}
}
]
},
Here is the C# code:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
Log.Information("Starting up ...");
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext());
...
}
...
My goal is to be able to tweak the configuration file without recompiling the code, if I want to change the log level.
The above configuration sends Debug logs to both the console and the file, though I am trying to send only Info to the console. Does not work - both sinks show Debug.
The version I use is Serilog.AspNetCore 6.1.0
What am I missing?
答案1
得分: 1
restrictedToMinimumLevel
应该放在 Args 中,根据这个文档:
"Serilog": {
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"restrictedToMinimumLevel": "Information"
}
},
{
"Name": "File",
"Args": {
"path": "../logs/log-.txt",
"rollingInterval": "Day"
}
}
]
}
尝试了一个最小示例:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.CreateLogger();
Log.Debug("Debug");
Log.Information("Info");
它按预期工作:
英文:
restrictedToMinimumLevel
should be inside Args accroding to this doc:
"Serilog": {
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"restrictedToMinimumLevel": "Information"
}
},
{
"Name": "File",
"Args": {
"path": "../logs/log-.txt",
"rollingInterval": "Day"
}
}
]
},
Tried with a minimal example:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.CreateLogger();
Log.Debug("Debug");
Log.Information("Info");
it works as expected:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论