我找不到一个关于如何修改配置文件的示例,以便我可以输出传递给嵌套诊断上下文的字符串.
这是C#代码:
using (NLog.NestedDiagnosticsContext.Push(DateTime.Now.Ticks.ToString()))
{
//some logging
}
这是web.config中的nLog条目(不知道如何修改它):
<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>
解决方法:
刚想通了.找到了MDC的例子.刚刚为NDC调整了一下.这是更新的配置:
<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>