0

I'm new to .NET core. How to get the correlation ID to log the response using NLog?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Duplicate of https://stackoverflow.com/questions/30593801/approach-for-tying-all-nlog-logs-back-to-the-original-request-within-webapi/54854029#54854029 ? – Rolf Kristensen Feb 28 '20 at 19:12

2 Answers2

2

You could use ${activityid} to render System.Diagnostics trace correlation id.

Or if you need request.GetCorrelationId(), you could do this:

You could do this:

// using NLog.Web package

// register as soon as possible
AspNetLayoutRendererBase.Register("myCorrelationId", 
      (logEventInfo, httpContext, loggingConfiguration)
           => httpContext.Request.GetCorrelationId()); 

and render like ${myCorrelationId}

Julian
  • 33,915
  • 22
  • 119
  • 174
0

Not sure what is meant by "CorrelationId" but you can do this with NLog.Web.AspNetCore:

layout="${activityid:whenEmpty=${mdlc:item=RequestId:whenEmpty=${aspnet-TraceIdentifier}}}"

And if you are using JsNLog then you can do this:

layout="${aspnet-request:header=JSNLog-RequestId:whenEmpty=${mdlc:item=RequestId:whenEmpty=${aspnet-TraceIdentifier}}}"
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70