How to Configure NLog
  • 2 Minutes to read
  • Dark
  • PDF

How to Configure NLog

  • Dark
  • PDF

Article summary

Use NLog to send your application logs to Retrace. Follow these steps:

  1. Add our NuGet package
  2. Add our logging target to your NLog configuration
  3. Enter your Stackify license key

The code for Stackify's .NET library and logging appenders are all available on Github.

Getting Started Using NuGet

To add our NLog to your project, install our NuGet package.

PM> Install-Package NLog.Targets.Stackify

Add the Stackify ApiKey to your configuration

To complete the NuGet installation, copy your Stackify Activation Key (license key) from your Stackify account page and paste it here.

Please note that this example is for a .NET Framework config file. If you are using .NET Core, the configuration is the same but in JSON.

    <add key="Stackify.ApiKey" value="Your Activation Key" />
    <add key="Stackify.AppName" value="Your App Name"/> <!-- optional - will be inferred by Stackify if this is running on a monitored server -->
    <add key="Stackify.Environment" value="Your Environment"/> <!-- optional - will be inferred by Stackify if this is running on a monitored server -->

This installation does not require the AppName and Environment name. However, we recommend it if you are logging from a server without a Retrace agent.

Finish the process to complete the installation and configuration of the Stackify NLog appender.

Basic Appender Configuration

Here is the basic NLog configuration:

<nlog xmlns:xsi="">
    <target xsi:type="StackifyTarget" name="stackify"/>
      <logger name="*" minlevel="Debug" writeTo="stackify"/>

Logging Custom Properties

To add custom properties to your logs, use one of the overloaded logger methods. Choose the level of log, the message, and add the needed properties.

Execute the following code:

var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Debug("User Login", new { ID = 65465849, User = "Stackify" });

The output is as follows:


Recording Context Values

Using logical or thread context keys within NLog is possible. You can also collect and send it to Retrace.

Review the NLog documentation to learn more about using contexts.

GlobalDiagnosticContext.Set(“UserID”, 659849);
MappedDiagnosticsLogicalContext.Set(“Contect”, context.ToString());
NLog.LogManager.Shutdown();  //must be called before you exit your app to ensure that messages are flushed to Stackify

In your configuration:

<target name="stackify" type="StackifyTarget" includeMdlc="true" includeEventProperties="true" layout="${message}">
  <contextproperty name="AppId" layout="${gdc:item=AppId}" />
  <contextproperty name="NestedDiagnosticsLogicalContext" layout="${ndlc}" />
  <contextproperty name="ThreadId" layout="${threadid}" />
  <contextproperty name="TraceActivityId" layout="${activityid}" />
  <contextproperty name="MessageTemplate" layout="${message:raw=true}" />

Use NestedDiagnosticLogicalContext (NDLC) if you will not use a custom layout. This will be automatically added to Stackify once it is targeted. It will not require any layout or editing in your config file.

Control which Logs are being sent to Stackify

You can alter the logging level for Stackify. This can be done if you want to continue logging to other targets aside from Stackify.

Inside the NLog file, add a new rule by replacing ‘Info’ with your preference and ‘stackify’ with the target name.

    <target name="textfile" xsi:type="File" fileName="logfile.txt"	/>
    <target xsi:type="StackifyTarget" name="stackify"  />
    <logger name="*" minlevel="Debug" writeTo="textfile"/>
    <logger name="*" minlevel="Info" writeTo="stackify" />

View your Logs with Prefix

You can use Prefix to view your logs. It requires adding the NLog appender to pick up and show your logs within Prefix. Stackify ApiKey configuration is not needed for Prefix to work properly.


Check with Troubleshoot: Errors and Logs .NET Configurations, if you have issues getting Errors and Logs.

Was this article helpful?