Approach for tying all NLog logs back to the original request within WebAPI?

后端 未结 2 681
广开言路
广开言路 2021-01-19 09:49

I am working to build an API using WebAPI, and have been using NLog for logging throughout the stack. My API solution has two main projects including:

  • The webs
相关标签:
2条回答
  • 2021-01-19 10:21

    Take a look at LogicalCallContext. As of .NET 4.5, it supports async scenarios.

    Mr. Jeffrey Richter:

    The .NET Framework has a little-known facility that allows you to associate data with a “logical” thread-of-execution. This facility is called logical call context and it allows data to flow to other threads, AppDomains, and even to threads in other processes.

    0 讨论(0)
  • 2021-01-19 10:42

    NLog.Extension.Logging ver. 1.0 is able to capture context-properties created with ILogger.BeginScope. These can be extracted using NLog ${mdlc}.

    Microsoft engine will by default inject properties like RequestId, RequestPath, etc.

    See also: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-properties-with-Microsoft-Extension-Logging

    0 讨论(0)
提交回复
热议问题