DocumentationDiscussions
DocumentationDiscussions

NLog

NLog is a flexible and mature logging implementation for .NET. Since NLog 4.5, structured logging with message templates is fully supported, making Seq and NLog a powerful combination.

Installing the Target

First, install the NLog.Targets.Seq NuGet package. For example, at the Visual Studio Package Manager console, type:

PM> Install-Package NLog.Targets.Seq

This will add the required assemblies to the project.

Then, add the target to your NLog configuration. You'll first need to add the NLog.Targets.Seq assembly:

<?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">

  <extensions>
    <add assembly="NLog.Targets.Seq"/>
  </extensions>

And then targets and rules entries:

<targets>
    <target name="seq" xsi:type="BufferingWrapper" bufferSize="1000" flushTimeout="2000">
      <target xsi:type="Seq" serverUrl="http://localhost:5341" apiKey="" />
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="seq" />
  </rules>

Set the serverUrl value to the address of your Seq server.

The BufferingWrapper ensures that writes to Seq do not block the application.

Writing Events

That's it! When you write log events to your NLog logger:

logger.Info("Hello, {Name}, from NLog!", Environment.UserName);

They'll appear beautifully in Seq, complete with queryable properties.

Enriching events

To add properties like thread ID or machine name, use <property> elements in the target configuration:

<target name="seq" xsi:type="Seq" serverUrl="http://localhost:5341" apiKey="">
      <property name="ThreadId" value="${threadid}" as="number" />
      <property name="MachineName" value="${machinename}" />
    </target>

Any NLog layout expression can be used for the property value. Use as="number" to attempt numeric conversion of a value such as thread ID above.


Did this page help you?