• Print
  • Share
  • Dark
    Light

How to Configure NLog

  • Updated on 15 Jun 2018
  • 3 minutes to read
  • Contributors

You can easily send your application logs to Retrace if you are using NLog.

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

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

Getting Started Using NuGet

Install our nuget package to add our NLog target to your project.

PM> Install-Package NLog.Targets.Stackify

Add the Stackify ApiKey to your config

In order for the NuGet installation to complete, 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 would be done in the same manner in json.

<appSettings>
    <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 -->
</appSettings>

The AppName and Environment are not required but are recommended if you are logging from a server without Retrace installed.

Once this is complete, you are done with the installation and configuration of the Stackify NLog appender.

Basic Appender Configuration

Below is the simplest NLog configuration needed to log to Retrace.

<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target xsi:type="StackifyTarget" name="stackify"/>
  </targets>
  <rules>
      <logger name="*" minlevel="Debug" writeTo="stackify"/>
  </rules>
</nlog>

Logging Custom Properties

In order to add custom properties to your logs, use one of the overloaded logger methods, as shown below. Choose the level of log, the message, and add as many additional properties as needed in the fashion shown.

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

This is what the above example should look like in Retrace after being run.

image.png

Recording Context Values

If you are using logical or thread context keys within NLog and would like to collect and send those to Retrace, you can.

Review the NLog documentation to learn more about using contexts.

GlobalDiagnosticsContext.Set("UserID", 659849);
MappedDiagnosticsContext.Set("Context", context.ToString());
NestedDiagnosticsContext.Push("clientid");

In your config:

<target xsi:type="StackifyTarget" name="stackify" layout="${mdc:item=Context} | ${ndc} | ${gdc:item=UserID}"/>

If you would prefer not to use a custom layout, use NDC, as it will be automatically added when Stackify is targeted without requiring a layout or any editing in your config file.

Control Which Logs are Sent to Stackify

If you have other logging targets, in addition to Stackify, that you want to continue logging to, but you want to log at different levels to each target, you can easily alter your logging level for Stackify. In the NLog.config file, add a new rule as shown below, replacing ‘Info’ with whatever level you’d like, and ‘stackify’ with your target name.

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

Recording Context Values

If you are using logical or thread context keys within NLog and would like to collect and send those to Retrace, you can. You can review our code to get a better of how it works. Use the NDC or GDC to add a context property to any log you would like.

NestedDiagnosticsContext.Push("clientid");
GlobalDiagnosticsContext.Set("UserID", 659849);

NDC doesn't require any additions to the config file. When pushed to Retrace the above example would look like this:

image.png

GDC requires adding a line to your Stackify target in the config file:

globalContextKeys="UserID,test"

This is what the GDC would look like in Retrace:

image.png

View Your Logs With Prefix

You can use Prefix to view your logs on your workstation. It requires adding the NLog appender to pick up and show your logs within Prefix. You don't even need to configure a Stackify ApiKey for Prefix to work properly!

Troubleshooting

If you are having issues getting Errors and Logs to come into your account, please see Troubleshoot: Errors and Logs .NET Configurations