我被要求写日志(log)级别的信息,警告和开发中的错误,但每一个都在一个单独的文件中,所以我有information.txt
只包含信息日志(log),warning.txt
只包含警告日志(log),Error.txt
只包含错误日志(log).
这是我目前的实现(它起作用了).
在Program.cs
年中:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseSerilog((webHostBuilderContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(webHostBuilderContext.Configuration);
});
});
在应用程序设置中:
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "wwwroot/Logs/log.txt",
"rollingInterval": "Day",
"reatinedFileCountLimit": 14,
"restrictedToMinimumLevel": "Information"
}
}
]
},
我已经安装了Serilog.Expressions包并阅读了文档,我知道我必须使用这样的东西
"Filters": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "@l = 'Information'"
}
}
]
但我不明白它应该放在哪里.我试着把它放在Args
或Args
到Name
之间,但都不起作用,有人能帮我吗?
我已经在Stackoverflow上阅读了很多帖子,但都没有奏效:(