Serilog 与 Asp.net Web Api 不使用丰富器

dmo*_*181 6 c# asp.net asp.net-web-api serilog

我有一个 Microsoft Asp.Net Web Api 项目,我试图在其中设置 serilog 日志记录。我已经安装了 Serilog、Serilog.Sinks.File、SerilogWeb.Classic 和 SerilogWeb.Classic.WebApi 的 Nuget 包。请注意,我无法使用 .Net Core。

我的问题是,我尝试使用 SerilogWeb.CLassic.WebApi 包中的丰富器,但它们没有改变我的输出。我确信我所做的只是一个简单的错误,但我在网上找不到其他人使用相同丰富器的任何示例(我找到的所有内容都在 .Net Core 上)。

Log.Logger = new LoggerConfiguration()
    .Enrich.WithWebApiActionName()
    .WriteTo.File("D:/mytestresults.txt")
    .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

上面的代码片段来自我的 Startup.cs 文件。当我运行时,http 请求会记录到文件中,但添加和删除 Enrich 行没有任何区别。以下是示例文件输出

2019-03-26 10:09:11.215 -04:00 [INF] HTTP GET /api/actions/ responded 200 in 17ms
2019-03-26 10:09:13.621 -04:00 [INF] HTTP GET /api/actions/ responded 200 in 9ms
Run Code Online (Sandbox Code Playgroud)

请注意,我确实想使用其他丰富器,但我已经简化了一些,以便找出我做错了什么。

为了清楚地了解我正在使用的包,以下是我的一些packages.config

<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net461" />
<package id="Serilog" version="2.8.0" targetFramework="net461" />
<package id="Serilog.Sinks.File" version="4.0.0" targetFramework="net461" />
<package id="Serilog.Sinks.MSSqlServer" version="5.1.2" targetFramework="net461" />
<package id="Serilog.Sinks.PeriodicBatching" version="2.1.1" targetFramework="net461" />
<package id="SerilogWeb.Classic" version="5.0.48" targetFramework="net461" />
<package id="SerilogWeb.Classic.WebApi" version="4.0.5" targetFramework="net461" />
Run Code Online (Sandbox Code Playgroud)

编辑

以下是建议更改后的代码,虽然一些丰富器正在工作,但我没有获得操作和控制器名称:

        Log.Logger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .Enrich.WithUserName()
        .Enrich.WithWebApiRouteData()
        .Enrich.WithWebApiControllerName()
        .Enrich.WithWebApiRouteTemplate()
        .Enrich.WithWebApiActionName()
        .Enrich.WithHttpRequestUrl()
        .WriteTo.MSSqlServer(connectionString, tableName, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information, autoCreateSqlTable: true)
        .WriteTo.File("D:/mynewlog.txt", outputTemplate: "<{WebApiAction}> Time: {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} Level:[{Level:u3}] Message:{Message:lj}Properties:{Properties}{NewLine}{Exception}")
        .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

以下是此时记录到文件中的内容(sql 相同)

<> Time: 2019-03-27 10:34:47.842 -04:00 Level:[INF] Message:HTTP GET /api/actions/reviews responded 200 in 7msProperties:{ SourceContext: "SerilogWeb.Classic.ApplicationLifecycleModule", UserName: "theUsernameWasHere", HttpRequestUrl: "http://localhost:61111/api/actions/reviews" }
Run Code Online (Sandbox Code Playgroud)

tsi*_*lar 3

我认为现在发生的情况是您的“日志事件”已正确丰富了所有信息,但接收File器未正确配置为显示该信息。

接收器的默认输出模板File{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} \[{Level:u3}\] {Message:lj}{NewLine}{Exception},这意味着它将正确显示“渲染的消息”(消息模板 + 消息模板中引用的属性值)。

您需要做的是将其配置为同时显示所有额外属性(丰富每个日志事件时添加到该日志事件的属性)。

对于File接收器,您可以在输出模板中添加特殊{Properties}指令,这将显示附加到事件的所有属性,但不会呈现为消息模板的一部分。

那看起来像:

Log.Logger = new LoggerConfiguration()
    .Enrich.WithWebApiActionName()
    .WriteTo.File("D:/mytestresults.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} \[{Level:u3}\] {Message:lj}{Properties}{NewLine}{Exception}")
    .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

(或者{Properties:j}将它们格式化为 JSON)。

这将输出附加到事件但不位于输出模板中其他任何位置的所有属性。

如果您只对 感兴趣WebApiAction,您也可以执行以下操作:

Log.Logger = new LoggerConfiguration()
    .Enrich.WithWebApiActionName()
    .WriteTo.File("D:/mytestresults.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} \[{Level:u3}\]<{WebApiAction}> {Message:lj}{NewLine}{Exception}")
    .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

...然后你会在输出中得到这样的行:

2019-03-26 10:09:13.621 -04:00 [INF]<MyActionName> HTTP GET /api/actions/ responded 200 in 9ms
Run Code Online (Sandbox Code Playgroud)

更新

看起来这不是“输出格式”问题,因为您可以正确显示一些附加属性......

.WithUserName并且.WithRequestUrl不是特定于 WebApi 的,可以为任何ASP.NET 应用程序轻松收集。由于这一行,这些.WithWebApi*扩展依赖于我们在启动时IAuthenticationFilter在 Web API 中声明的一个...

以下是可以解释缺乏丰富性的可能情况: - 由于某种原因,PreApplicationStartMethod程序集属性无法按预期工作 - 由于某种原因,StoreWebApInfoInHttpContextAuthenticationFilter在 Web API 中注册不起作用 - 由于某种原因,我们无法从ActionContext

AuthenticationFilter您是否可能在启动时使用注册的 s 做“奇怪”的事情?

最好尝试将其作为您在此处报告的问题的一部分进行追踪: https: //github.com/serilog-web/classic-webapi/issues/18 ;)