dustinchilson / NLog.Targets.GraylogHttp

NLog target that pushes log messages to Graylog using the Http input.
Apache License 2.0
19 stars 11 forks source link
gelf graylog logging netstandard nlog

NLog.Targets.GraylogHttp

Codacy Badge Apache 2.0 licensed NuGet

This is a custom target for NLog that pushes log messages to Graylog using the Http input.

This library was influenced by EasyGelf

Installation

This library is packaged as a nuget package available here

Install-Package NLog.Targets.GraylogHttp

NetStandard, .Net Core 2.1-5.0, .Net Framework 4.5

This library runs under netstandard 1.3 and fully supports .Net Core 2.1-3.1, .Net 5.0, and .Net Framework 4.5+

Usage

Add or modify your NLog Configuration to add the new target and Extension Assembly.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <extensions>
    <add assembly="NLog.Targets.GraylogHttp"/>
  </extensions>
  <targets>
    <target name="graylog"
              xsi:type="GraylogHttp"
              facility="[FACILITY]"
              graylogServer="[SERVER]"
              graylogPort="[PORT]">
        <!-- Location information. -->
        <parameter name="source_method" layout="${callsite}" />
        <parameter name="source_line" layout="${callsite-linenumber}" />

        <parameter name="test_prop" layout="${event-context:item=test_prop}" />
      </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" appendTo="graylog"/>
  </rules>
</nlog>

Simple Logging

var logger = LogManager.GetCurrentClassLogger();
logger.Trace("String");
logger.Debug("String");
logger.Warn("String");
logger.Error("String");
logger.Fatal("String");

Advanced Properties

var logger = LogManager.GetCurrentClassLogger();
var e = new LogEventInfo(LogLevel.Fatal, "Test", "Test Message");
e.Properties["test_prop"] = "test property";
logger.Log(e);

Note

In order to receive logs in your Graylog server, make sure you create Input for your application mentioned in Getting Started guide under Collect Messages link.

References

Gelf Spec