gpt4 book ai didi

c# - 使用 appsettings.json 配置 Serilog RollingFile

转载 作者:可可西里 更新时间:2023-11-01 07:54:43 25 4
gpt4 key购买 nike

我正在尝试为 .NET Core 项目配置 Serilog。这是我的 appsettings.json 中的内容:

 "Serilog": 
{
"MinimumLevel": "Verbose",
"Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId"],
"WriteTo": [
{ "Name": "RollingFile",
"Args": {
"pathFormat": "C:/Logfiles/testapp/log-{Date}.json",
"textFormatter": "JsonFormatter",
"fileSizeLimitBytes": 2147483648,
"retainedFileCountLimit": 5
}
}
]
}

我看到的问题是 JsonFormatter 没有被拾取,而是我使用默认的文本格式化程序获取条目。我尝试使用 "formatter": "JsonFormatter",但得到了相同的结果。

如果我在代码中配置 Serilog,一切正常:

var jsonSink = new RollingFileSink(config["Logger:FilePath"], new JsonFormatter(), 2147483648, 5);

var logger = new Serilog.LoggerConfiguration().WriteTo.Sink(jsonSink);

这是我的 project.json 的相关部分:

"Serilog": "2.2.1",
"Serilog.Extensions.Logging": "1.1.0",
"Serilog.Sinks.Literate": "2.0.0",
"Serilog.Sinks.Seq": "2.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Serilog.Enrichers.Thread": "2.0.0",
"Serilog.Enrichers.Process": "2.0.0",
"Serilog.Sinks.ColoredConsole": "2.0.0",
"Serilog.Settings.Configuration": "2.2.0"

最佳答案

formatter 参数需要是一个完全限定的类型名称。尝试:

"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"

关于c# - 使用 appsettings.json 配置 Serilog RollingFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40880261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com