gpt4 book ai didi

c# - Serilog:如何进一步增强 appsettings.json 中的过滤器表达式?

转载 作者:行者123 更新时间:2023-11-30 22:56:40 29 4
gpt4 key购买 nike

我有两个带有 Serilog 的记录器,它们保存到不同的文件中。我想通过 appsettings.json 创建记录器,而不是代码。

当我通过 Startup.cs 配置创建我的记录器时,我得到了一个更具体的日志,我更喜欢这样,因为它更清晰,并且允许其他人检查日志和无需太多技术知识即可了解正在发生的事情。但是,我想将其应用于应用程序设置,以便我可以将代码迁移到我可能使用的任何其他项目。仅供引用:我不想覆盖日志级别。

例如,我使用如下代码创建我的记录器:

Log.Logger = new LoggerConfiguration()
.WriteTo.ColoredConsole()
.WriteTo.Logger(lc => lc
.Filter.ByExcluding(Matching.FromSource("Microsoft"))
.WriteTo.File("Serilogs/Program_log_.txt", rollingInterval: RollingInterval.Day)
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information))

.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(Matching.FromSource("Microsoft"))
.WriteTo.File("Serilogs/ServerTrace_log_.txt", rollingInterval: RollingInterval.Day)
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information))

.CreateLogger();

这会生成两个独立的输出,我喜欢它们的格式:文件 1:可读的自定义日志:

2019-01-18 11:18:09.873 +00:00 [INF] //Some custom log information
2019-01-18 11:18:09.875 +00:00 [INF] //Some more custom log information

文件 2:堆栈跟踪,服务器信息:

2019-01-18 11:17:56.378 +00:00 [INF] User profile is available. Using '//A path' as key repository and Windows DPAPI to encrypt keys at rest.
2019-01-18 11:17:57.840 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/index.html
2019-01-18 11:17:58.074 +00:00 [INF] Request finished in 235.5796ms 200 text/html
2019-01-18 11:17:58.326 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/v1/swagger.json
2019-01-18 11:17:58.452 +00:00 [INF] Request finished in 126.2127ms 200 application/json;charset=utf-8

当我使用 JSON 应用程序设置时,它看起来像这样:

 "Serilog": {
"Using": [
"Serilog.Sinks.File",
"Serilog.Filters.Expressions",
"Serilog.Settings.Configuration"
],
"WriteTo": [
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncluding",
"Args": {
"expression": "SourceContext = 'Microsoft'"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "./Serilogs/server_logs_.log",
"rollingInterval": "Day"
}
}
]
}
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "SourceContext = 'Microsoft.AspNetCore.Hosting.Internal.WebHost'"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "./Serilogs/program_logs_.log",
"rollingInterval": "Day"
}
}
]
}
}
}
]
}

这会生成两个文件。文件 1:可读的自定义日志:

2019-01-18 11:22:52.903 +00:00 [INF] User profile is available. Using '// A path' as key repository and Windows DPAPI to encrypt keys at rest.
2019-01-18 11:22:59.229 +00:00 [INF] Route matched with {action = "PollForStatusUpdate", controller = "StatusMessage"}. Executing action {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname})
2019-01-18 11:22:59.505 +00:00 [INF] Executing action method {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) with arguments (["{apiname}.{controller}.{method}.MessageResult"]) - Validation state: "Valid"
2019-01-18 11:22:59.632 +00:00 [INF] //Some custom log information
2019-01-18 11:22:59.639 +00:00 [INF] //Some more custom log information
{apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 516.8129ms.
2019-01-18 11:23:00.040 +00:00 [INF] Executing ObjectResult, writing value of type '{apiname}.{controller}.{method}.MessageResult'.
2019-01-18 11:23:00.048 +00:00 [INF] Executed action {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) in 813.08940000000007ms

文件 2:堆栈跟踪,服务器信息:

2019-01-18 11:22:52.903 +00:00 [INF] User profile is available. Using 'C:\Users\msharp\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
2019-01-18 11:22:54.263 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/index.html
2019-01-18 11:22:54.473 +00:00 [INF] Request finished in 211.831ms 200 text/html
2019-01-18 11:22:54.737 +00:00 [INF] Request starting HTTP/1.1 GET https://localhost:5001/swagger/v1/swagger.json
2019-01-18 11:22:54.838 +00:00 [INF] Request finished in 100.8892ms 200 application/json;charset=utf-8
2019-01-18 11:22:59.205 +00:00 [INF] Request starting HTTP/1.1 POST https://localhost:5001/api/StatusMessage/PollForStatusUpdate application/json-patch+json 180
2019-01-18 11:22:59.229 +00:00 [INF] Route matched with {action = "PollForStatusUpdate", controller = "StatusMessage"}. Executing action PureValuationsApi.Controllers.StatusMessageController.PollForStatusUpdate (PureValuationsApi)
2019-01-18 11:22:59.505 +00:00 [INF] Executing action method {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) with arguments (["{apiname}.{controller}.{method}.MessageResult"]) - Validation state: "Valid"
2019-01-18 11:22:59.632 +00:00 [INF] //Some custom lof information
2019-01-18 11:22:59.639 +00:00 [INF] //Some more custom log information
{apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 516.8129ms.
2019-01-18 11:23:00.040 +00:00 [INF] Executing ObjectResult, writing value of type '{apiname}.{controller}.{method}.MessageResult'.
2019-01-18 11:23:00.048 +00:00 [INF] Executed action {apiname}.{controller}.{method}.PollForStatusUpdate ({apiname}) in 813.08940000000007ms
2019-01-18 11:23:00.052 +00:00 [INF] Request finished in 847.5964ms 200 application/json; charset=utf-8

因此,通过应用感觉相同的技术,结果的结果是不同的,因为 File 1 现在包含更多操作信息,而 File 2 包含堆栈 加上自定义日志信息,尽管我希望它们完全分开。

最佳答案

我认为您的过滤器不太正确。

过滤器表达式应该是:

"expression": "SourceContext = 'Microsoft' or StartsWith(SourceContext, 'Microsoft.')"

(我猜你可以只做 StartsWith(SourceContext, 'Microsoft') 而没有最后一个点,但是这样对于像 MicrosoftOrIsItReally.MyNamespace 这样的命名空间可能表现不正确)

(内部 Matching.ForSource 进行了一种 SourceContext.StartsWith(..) 过滤,如 in the source 所示)

要确认它,您可能需要编辑 outputTemplate您的文件接收器显示 SourceContext属性并查看日志事件的来源。 (默认值为 "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")。您可以将其更改为 "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}]<{SourceContext}> {Message:lj}{NewLine}{Exception}"包括 SourceContext属性(property)。


顺便说一句,你有一个额外的,不需要的"Using"指示。 "Serilog.Settings.Configuration"没必要

关于c# - Serilog:如何进一步增强 appsettings.json 中的过滤器表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54253281/

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