Reputation: 109
In my .Net 4.8 MVC application I'm trying to write logs with NLog. One of the field is traceId - ${aspnet-TraceIdentifier}, which suppose to be supported by NLog.Web.
So I followed the documentation and installed:
I placed my NLog.config in project's directory:
<?xml version="1.0" ?>
<nlog autoReload="true"
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true"
internalLogLevel="trace"
internalLogFile="c:\test\internal-nlog.txt">
<targets>
<target name="ws" xsi:type="WebService" protocol="JsonPost"
url="https://log-api.com/log/v1">
<parameter name="time" layout="${date}" />
<parameter name="level" layout="${level:upperCase=true}"/>
<parameter name="message" layout="${message}" />
<parameter name="host" layout="${machinename}" />
<parameter name="threadid" layout="${threadid}" />
<parameter name="ActivityId" layout="${activityId}" />
<parameter name="processId" layout="${processId}" />
<parameter name="threadid" layout="${threadid}" />
<parameter name="event-properties" layout="${all-event-properties}" />
<parameter name="exception" layout="${exception}" />
<parameter name="traceId" layout="${aspnet-TraceIdentifier}" />
<parameter name="environment" layout="Production" />
<parameter name="serviceName" layout="company-api2222222" />
<parameter name="session" layout="${aspnet-session}"/>
<parameter name="sessionId" layout="${aspnet-sessionid}"/>
<header name="X-License-Key" layout="${environment:LICENSE_KEY}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="ws" />
</rules>
</nlog>
According to the documentation, further configuration is not needed: "Simply install the package. NLog will detect the extension automatically."
So, inside my controller I simply getting current class logger and logging:
NLog.LogManager.GetCurrentClassLogger().Info("Hello World");
But unfortunately, ${aspnet-TraceIdentifier} value is empty, and the worst thing is that I cannot find any documentation about how to make it works.
I tried to put extensions tag above targets in NLog.config file
<extensions>
<add assembly="NLog.Web"/>
</extensions>
and even tried this as well:
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
But nothing changed. Can anyone help? There is something that I am missing? maybe a configuration problem after all?
Upvotes: 1
Views: 2278
Reputation: 36750
It's unclear if you're using ASP.NET or ASP.NET Core.
So an answer for both:
For ASP.NET you should use <add assembly="NLog.Web"/>
and the NLog.Web package
internally HttpWorkerRequest.RequestTraceIdentifier
will be used.
For ASP.NET Core you should use <add assembly="NLog.Web.AspNetCore"/>
and the NLog.Web.AspNetCore package
NLog uses these for the traceindentifier:
ASP.NET Core 3-5: use System.Diagnostics.Activity.Current.Id
(default) or HttpContext.TraceIdentifier
ASP.NET Core 1+2: HttpContext.TraceIdentifier
For ASP.NET Core 3-5 you maybe need ignoreActivityId. e.g. ${aspnet-TraceIdentifier:ignoreActivityId=true}
- if true - HttpContext.TraceIdentifier
will be used instead of System.Diagnostics.Activity.Current.Id
Upvotes: 0