c# - 带有 Asp.net Web Api 的 Serilog 不使用 enricher

标签 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 包中的增强器,但它们并没有改变我的输出。我确信我正在做的是一个简单的错误,但我找不到任何其他人在网上使用同样的 enricher 的例子(我找到的所有东西都在 .Net Core 上)。

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

上面的代码片段来 self 的 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

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

为了清楚地了解我正在使用的包,以下是我的一些 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" />

编辑

以下是建议更改后的代码,虽然一些 enricher 正在运行,但我没有获得操作和 Controller 名称:

        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();

下面是此时记录到文件中的内容(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" }

最佳答案

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

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

您需要做的是以所有额外属性(丰富时添加到每个日志事件的属性)也显示的方式配置它。

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

那看起来像:

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();

(或 {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();

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

2019-03-26 10:09:13.621 -04:00 [INF]<MyActionName> HTTP GET /api/actions/ responded 200 in 9ms

更新

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

.WithUserName.WithRequestUrl 不是特定于 WebApi 的,可以为 任何 ASP.NET 应用程序轻松收集。 .WithWebApi* 扩展依赖于 IAuthenticationFilter我们在 Web API 中声明 on start up谢谢to this line ...

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

中提取信息

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

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

关于c# - 带有 Asp.net Web Api 的 Serilog 不使用 enricher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55359440/

相关文章:

c# - 如何检索 html li 值

C# 发送带有附件的电子邮件(图片)

c# - 将 JSON 列从 MySql 数据库映射到来自 Web Api 的 C# 类

c# - 如何在 WPF 中使用左右键在 tabitem 之间导航

c# - 在编辑 mysql 数据库后运行某种脚本 C#

c# - 按反射排序比按成员排序更快?

c# - 如何编写此 Linq to NHibernate 查询

asp.net - 弹出窗口中的Ajax评级控件

list - Web API 使用附加属性扩展列表

asp.net - Web API - Web 窗体项目安全错误