سؤال

How can I log the CorrelationManager.ActivityId as a separate field?

Here is my current 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>
هل كانت مفيدة؟

المحلول 2

Currently there is no built in support for CorrelationManager.ActivityId.

However you can create your own layout render which outputs it:

[LayoutRenderer("activityid")]
public class ActivityIdLayoutRenderer : LayoutRenderer
{
    protected override void Append(StringBuilder builder, LogEventInfo logEvent)
    {
        builder.Append(Trace.CorrelationManager.ActivityId.ToString());
    }
}

You also need to register it with:

 ConfigurationItemFactory.Default.LayoutRenderers
      .RegisterDefinition("activityid", typeof(ActivityIdLayoutRenderer));

Then you can use it in your target definition with:

<target xsi:type="Mongo">
      <!-- ... -->
      <field name="ActivityId" layout="${activityid}" />
</target>

As an alternative solution you can also you the Event Context Layout Renderer to output any custom property like the ActivityId but in this case you have to include it every time when you log something.

نصائح أخرى

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top