与Asp.net Web Api的Serilog不使用浓缩器 [英] Serilog with Asp.net Web Api not using enricher

查看:187
本文介绍了与Asp.net Web Api的Serilog不使用浓缩器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Microsoft Asp.Net Web Api project, in which I am attempting to setup serilog logging. I have installed the Nuget packages for Serilog, Serilog.Sinks.File, SerilogWeb.Classic, and SerilogWeb.Classic.WebApi. Note, that I am NOT able to use .Net Core.

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

My problem is that I am attempting to use the enrichers from the SerilogWeb.CLassic.WebApi package, but they aren't changing my output. I am sure that what I am doing is a simple mistake, but I cannot find any examples of others using this same enricher online (everything I find is on .Net Core).

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

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

The code snippet above is from my Startup.cs file. When I run the http requests are logged to the file, but adding and removing the Enrich line makes no difference. Below is a sample file output

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

请注意,我确实想使用其他浓缩器,但是为了弄清楚我做错了什么,我将其简化了一些.

Note, that I do want to use other enrichers, but I have simplified this down some for figuring out what I am doing wrong.

为使我清楚使用的软件包,以下是我的一些packages.config

For clarity about the packages that I am using, the following is some of my 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" />


编辑

以下是我提出更改后的代码,并且当一些浓缩器正在工作时,我没有得到动作和控制器名称:

The following is my code after changes suggested, and while some enrichers are working, I am not getting the action and controller names:

        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相同)

The following is what is being logged to the file at this point (sql is the same)

<> 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 未正确配置接收器以显示该信息.

I think that what is happening right now is that your "log events" are properly enriched with all the information, but the File sink is not configured properly to display that information.

File 接收器的默认输出模板

The default output template of the File sink is {Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} \[{Level:u3}\] {Message:lj}{NewLine}{Exception} , which means that it will properly display the "rendered message" (the message template + the value of the properties that are referenced in the message template).

您需要做的是配置它,以便同时显示所有其他属性(在丰富每个日志事件时添加到其中的属性).

What you need to do is to configure it in a way that all the extra properties (the ones that are added to each log event when it is enriched) are also displayed.

对于 File 接收器,您可以例如添加特殊的

For the File sink, you can for instance add the special {Properties} directive in you output template, and that will show all the properties that are attached to the event, but are not rendered as part of the message template.

这看起来像:

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).

(or {Properties:j} to have them formatted as JSON).

这将输出该事件附带的所有属性,但不在输出模板的其他位置.

This will output all the properties attached to the event but not located anywhere else in the output template.

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

If you are only interested in the WebApiAction, you could also do something like :

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

...,然后您将在输出中得到如下所示的行:

... and then you would end up with lines like this in the output :

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


更新

这似乎不是输出格式"问题,因为您可以正确显示一些附加属性...

It looks like this is not an "output formatting" issue, as you can properly display some attached properties ...

.WithUserName .WithRequestUrl 不是WebApi特定的,可以很容易地为任何 ASP.NET应用程序收集. .WithWebApi * 扩展依赖于

.WithUserName and .WithRequestUrl are not WebApi specific and can be easily collected for any ASP.NET application. The .WithWebApi* extensions rely on an IAuthenticationFilter that we declare in Web API on start up thanks to this line ...

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

Here are possible scenarios that could explain the lack of enrichment : - for some reason the PreApplicationStartMethod assembly attribute does not work as expected - for some reason registering the StoreWebApInfoInHttpContextAuthenticationFilter in Web API does not work - for some reason we cannot extract the information from the ActionContext

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

Are you possibly doing "strange" things with the registered AuthenticationFilters on start up ?

尝试将其作为您在此处报告的问题的一部分进行跟踪可能更好:

It's probably better to try and track it down as part of the issue your reported here : https://github.com/serilog-web/classic-webapi/issues/18 ;)

这篇关于与Asp.net Web Api的Serilog不使用浓缩器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆