Nlog: How to output the value of the context nested diagnostic code to a log file?

I cannot find an example of how to modify my configuration file so that I can output a string passed to the context of the nested diagnostics.

Here is the C # code:

using (NLog.NestedDiagnosticsContext.Push(DateTime.Now.Ticks.ToString())) { //some logging } 

and this is the nLog entry in the web.config file (don't know how to change this):

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- make sure to set 'Copy To Output Directory' option for this file --> <targets> <target name="logfile" xsi:type="File" fileName="somepath\\logfile.txt" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="logfile" /> </rules> </nlog> 
+4
source share
1 answer

Just figured it out. Found an example for MDC. Just slightly changed it for NDC. Here's the updated configuration:

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <variable name="mycontext" value="${ndc:item=mycontext} - ${longdate} - ${message} "/> <targets> <target name="logfile" xsi:type="File" fileName="somepath\\logfile.txt" layout="${mycontext}"/> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="logfile" /> </rules> </nlog> 
+5
source

Source: https://habr.com/ru/post/1486227/


All Articles