NLog Logstash Setup

Ship logs from your .NET or .NET Core application to Logstash

Follow the steps below to send your observability data to Logit.io

Logs

Use the Logit.io NLog configuration examples below to ship logs from your .NET or .NET Core application to Logstash.

Install Integration

Please click on the Install Integration button to configure your stack for this source.

Install

This guide assumes that Visual Studio 2019 is being used as the IDE (Integrated Development Environment). Type the relevant command below into the NuGet Package Manager console (you can find it under the Tools menu -> NuGet Package Manager -> Package Manager Console).

If you are using a different IDE please consult the NLog wiki (opens in a new tab) for guidance.

Install-Package NLog

This will install the latest version. If you want to install a particular version you should append the version number you want to install, for example,

Install-Package NLog -Version 4.7.10

For further information check out the NLog documentation (opens in a new tab).

Configuring NLog

Configuring NLog can be done in your application app.config, web.config or a separate nlog.config file.

app.config/web.config Configuration

app.config/web.config
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
    <targets>
      <target
        xsi:type="Network"
        name="LogitTarget"
        address="tcp://@logstash.host:@logstash.sslPort"
        newLine="True">
        <layout type="JsonLayout" includeAllProperties="true">
          <attribute name="timestamp" layout="${longdate}" />
          <attribute name="logger" layout="${logger}" />
          <attribute name="machinename" layout="${machinename}" />
          <attribute name="threadid" layout="${threadid}" />
          <attribute name="level" layout="${level}"/>
          <attribute name="message" layout="${message}" />
          <attribute name="exception" layout="${exception}" />
        </layout>
      </target>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="LogitTarget" />
    </rules>
  </nlog>
</configuration>

nlog.config Configuration

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">
  <targets>
    <target
      xsi:type="Network"
      name="LogitTarget"
      address="tcp://@logstash.host:@logstash.sslPort"
      newLine="True">
      <layout type="JsonLayout" includeAllProperties="true">
        <attribute name="timestamp" layout="${longdate}" />
        <attribute name="logger" layout="${logger}" />
        <attribute name="machinename" layout="${machinename}" />
        <attribute name="threadid" layout="${threadid}" />
        <attribute name="level" layout="${level}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="exception" layout="${exception}" />
      </layout>
    </target>
  </targets>
 
  <rules>
    <logger name="*" minlevel="Trace" writeTo="LogitTarget" />
  </rules>
</nlog>

For further information check out the NLog documentation (opens in a new tab).

Using NLog

In order to emit log message you can simply call one of the methods on the Logger.

MyClass.cs
using NLog;
 
public class MyClass
{
    private static Logger logger = LogManager.GetCurrentClassLogger();
      
    public void MyMethod1()
    {
        logger.Info("Sample trace message");
 
        // below is an example of structured logging 
        logger.Info(
          "{user} logged in from IP address {ip_address}", 
          "GreatestUser", "127.0.0.1");
    }
}  

NLog is a fully featured logging platform for .NET developers that provides many quality of life benefits. For more configuration options please see the Nlog Documentation (opens in a new tab).

For more information about structured logging please see here (opens in a new tab) and here (opens in a new tab) for a comprehensive tutorial about setting up and using NLog.

Check Logit.io for your logs

Data should now have been sent to your Stack.

View My Data

If you don't see take a look at How to diagnose no data in Stack below for how to diagnose common issues.

How to diagnose no data in Stack

If you don't see data appearing in your stack after following this integration, take a look at the troubleshooting guide for steps to diagnose and resolve the problem or contact our support team and we'll be happy to assist.

NLog Logger Overview

NLog is a free open source logging platform for .NET & .NET Core, that is often compared to Serilog & Log4net. NLog is well known for being easy to use & is highly flexible alongside its high performance capabilities.

NLog also provides support for structured log data & covers a huge range of data targets and formats. NLog is able to note a variety of log severities including (by descending priority); Fatal, Error, Warn, Info, Debug & Trace.

While NLog itself is a logging platform, the service also creates internal application logs (opens in a new tab) of its own that many users find extremely hard to troubleshoot & parse without the use of a tool like Hosted Logstash.

Our platform offers Hosted Logstash alongside Elasticsearch & Kibana to help you view the entirety of your Nlog & .Net log files (among hundreds of other popular data sources) in a single source of truth.

If after following our NLog documentation you require any additional assistance with analysing your NLog or .Net logs we're here to help. Just reach out by contacting our support team via live chat and we'll be happy to help you get started.