2

Is there a way to enrich Serilog log data in ASP.NET Core 2.1 application, similar to how enrichers WithUserNameand WithHttpRequestUserAgent work for classic ASP.NET (System.Web)?

I tried implementing ILogEventEnricher interface, but it does not work, because I am not able to gain access to RequestContext from my custom enricher.

Nenad
  • 24,809
  • 11
  • 75
  • 93
  • 1
    See [Enrichment](https://github.com/serilog/serilog/wiki/Enrichment) and in your middleware use `LogContext.PushProperty("UserName", context.User.Identity.Name)`. – Mark G Jul 19 '18 at 23:42
  • 1
    @MarkG I was playing with this a bit and made it working, but I wonder if there can be threading issues? This is remark about `LogContext` says: "The scope of the context is the current logical thread, using AsyncLocal (and so is preserved across async/await calls).", so I wonder if it is safe to assume that `HttpRequest` is always bound to 1 logical thread? – Nenad Jul 20 '18 at 09:00
  • 1
    I'm left wondering the exact same thing. The Serilog documentation in this area is not at all good. – Neutrino Oct 23 '18 at 14:36
  • Does this point you in the right direction? https://stackoverflow.com/questions/51261177/add-username-into-serilog – IEnjoyEatingVegetables Apr 21 '19 at 03:12
  • 3
    Possible duplicate of [Add Username into Serilog](https://stackoverflow.com/questions/51261177/add-username-into-serilog) – IEnjoyEatingVegetables Apr 21 '19 at 03:12

1 Answers1

0

As pointed out in the comments it seems like Add Username into Serilog would serve your purpose and would also be the duplicate of this issue.

Jean-Paul
  • 380
  • 2
  • 9
  • 26