我的 ASPCoreNet 项目中有以下内容:
appsetting.json:
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": "logs/log-.txt",
"rollingInterval": "Day",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Properties:j}{NewLine}{Exception}"
}
}
]
}
},
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Properties:j}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
],
"Enrich": [ "WithMachineName", "WithThreadId", "FromLogContext" ]
},
"AllowedHosts": "*"
}
但是,当我从配置中读取时,我的记录器无法工作:
public class MyLogger
{
public static ILogger LoggingInstance
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
}
}
但是,这似乎有效:
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(a => a.File(
outputTemplate: ""[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Properties:j}{NewLine}{Exception}",
path: "logs/log-.txt",
rollingInterval: RollingInterval.Day
)).CreateLogger();
当我尝试从配置创建异步记录器时,它什么也不做。当我对它做同样的事情时,效果很好。
我实际上必须将异步包装到文件中,但做得不正确:
{
“Serilog”:{ “使用”:[ “塞里洛格”, “Serilog.Sinks.File”, “Serilog.Sinks.Async” ], "最低级别":"调试", “写入:异步”:{ "名称":"异步", “参数”:{ “配置”:[ { "名称":"文件", “参数”:{ "path":"project-log.txt", "rollingInterval":"天" } } ] } } } }