Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging Unique Request IDs in text file NLog - To differentiate between requests

Tags:

logging

nlog

I am using NLog logging mechanism in my application. The problem I face is when concurrent requests hit the application logging is done asynchronously and I am not able to identify which line belongs to which request. Is there a way in NLog to set the configuration to NLog itself logging a unique Request ID for each request ?

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
<targets>    
<!-- add your targets here -->
<target xsi:type="File" name="file"    fileName="E:\IBELogs\AirAvailability\AirAvailability.log" layout="${longdate}        ${uppercase:${level}} ${message}" />    
</targets>
<rules>
<!-- add your logging rules here --> 
<logger name="*" minlevel="Trace" writeTo="file" />    
</rules>
</nlog>
like image 276
Sadhurthan Avatar asked Jan 12 '23 21:01

Sadhurthan


1 Answers

In NLog 4.1 ${activityid} was introduced, see here. I cannot say MSDN documentation is clear enough about its value, but so far, this is doing the trick for us, since every request in our webAPI application gets assigned a unique GUID and it is preserved through the entire request as we see the same value in several Nlog operations in the same request.

like image 98
macebalp Avatar answered May 16 '23 06:05

macebalp