如何将CorrelationManager.ActivityId
记录为单独的字段?
这是我当前的NLog.config:
<?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">
<variable name="logDirectory" value="${basedir}/Logs" />
<extensions>
<add assembly="NLog.Mongo"/>
</extensions>
<targets>
<target xsi:type="Mongo"
name="mongoCustom"
includeDefaults="false"
connectionString="mongodb://localhost/FooLogging"
collectionName="authLogs">
<field name="Date" layout="${longdate:universalTime=true}" />
<field name="Level" layout="${level}"/>
<field name="Message" layout="${message}" />
<field name="Logger" layout="${logger}"/>
<field name="Exception" layout="${exception:format=tostring}" />
<field name="ThreadID" layout="${threadid}" />
<field name="ThreadName" layout="${threadname}" />
<field name="ProcessID" layout="${processid}" />
<field name="ProcessName" layout="${processname:fullName=true}" />
<field name="UserName" layout="${windows-identity}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="mongoCustom" />
</rules>
</nlog>
答案 0 :(得分:4)
目前,CorrelationManager.ActivityId
没有内置支持。
但是,您可以创建自己的布局渲染,并将其输出:
[LayoutRenderer("activityid")]
public class ActivityIdLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append(Trace.CorrelationManager.ActivityId.ToString());
}
}
您还需要注册:
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("activityid", typeof(ActivityIdLayoutRenderer));
然后您可以在目标定义中使用它:
<target xsi:type="Mongo">
<!-- ... -->
<field name="ActivityId" layout="${activityid}" />
</target>
作为替代解决方案,您还可以Event Context Layout Renderer输出任何自定义属性,例如ActivityId,但在这种情况下,每次登录时都必须包含它。
答案 1 :(得分:4)
$ {activityid}。 https://github.com/nlog/nlog/wiki/Trace-Activity-Id-Layout-Renderer