Send data via NLog to your Logstash instance provided by Logit.io

NLog

Ship logs from your .NET application to logstash

Step 1 - Install

Installing Serilog You will need to add the NLog, NLog.Schema and NLog.Config packages. This command will install the required dependencies.

Install-Package NLog.Config

Step 2 - Configuring NLog

Configuring NLog can be done in your application app.config or web.config.

<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <nlog throwExceptions="true">
    <targets>
      <target
        type="Network"
        name="LogitTarget"
        address="tcp://your-logstash-host:your-port"
        newLine="True">
        <layout type="JsonLayout">
          <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>

Step 3 - Using NLog

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

using NLog;

public class MyClass
{
  private static Logger logger = LogManager.GetCurrentClassLogger();
    
  public void MyMethod1()
  {
    logger.Info("Sample trace message");
  }
}

Nlog is a fully featured logging framework and has many other capabilities that are not detailed here, please see the Nlog Documentation for more information.

expand view

Expand View

compact view

Compact View

Return to Search
Sign Up