Configure log4net

Before you begin, you’ll need: log4net 2.0.8 or higher

Add the dependency to your project

If you’re on Windows, navigate to your project’s folder in the command line, and run this command to install the dependency.

Install-Package Logzio.DotNet.Log4net

If you’re on a Mac or Linux machine, you can install the package using Visual Studio. Select Project > Add NuGet Packages..., and then search for Logzio.DotNet.Log4net.

Configure the appender

You can configure the appender in a configuration file or directly in the code. Use the samples in the code blocks below as a starting point, and replace them with a configuration that matches your needs.

For a complete list of options, see the configuration parameters below the code blocks.👇

See the log4net documentation for more information on the log4net configuration file.

Option 1: In a configuration file

<log4net>
  <appender name="LogzioAppender" type="Logzio.DotNet.Log4net.LogzioAppender, Logzio.DotNet.Log4net">

    <!-- Replace these parameters with your configuration -->
    <token><<SHIPPING-TOKEN>></token>
    <type>log4net</type>
    <listenerUrl><<LISTENER-HOST>>:8071</listenerUrl>
    <bufferSize>100</bufferSize>
    <bufferTimeout>00:00:05</bufferTimeout>
    <retriesMaxAttempts>3</retriesMaxAttempts>
    <retriesInterval>00:00:02</retriesInterval>
    <debug>false</debug>

  </appender>

  <root>
    <level value="INFO" />
    <appender-ref ref="LogzioAppender" />
  </root>
</log4net>

Option 2: In the code

var hierarchy = (Hierarchy)LogManager.GetRepository();
var logzioAppender = new LogzioAppender();

// Replace these parameters with your configuration
logzioAppender.AddToken("<<SHIPPING-TOKEN>>");
logzioAppender.AddType("log4net");
logzioAppender.AddListenerUrl("<<LISTENER-HOST>>:8071");
logzioAppender.AddBufferSize("100");
logzioAppender.AddBufferTimeout("00:00:05");
logzioAppender.AddRetriesMaxAttempts("3");
logzioAppender.AddRetriesInterval("00:00:02");
logzioAppender.AddDebug("false");

hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Configured = true;
Parameters
Parameter Description
token Your Logz.io account token.
Replace <<SHIPPING-TOKEN>> with the token of the account you want to ship to.
listenerUrl https://listener.logz.io:8071 Listener URL and port.
Replace <<LISTENER-HOST>> with your region’s listener host (for example, listener.logz.io). For more information on finding your account’s region, see Account region.
type log4net The log type, shipped as type field. Used by Logz.io for consistent parsing. Can’t contain spaces.
bufferSize 100 Maximum number of messages the logger will accumulate before sending them all as a bulk.
bufferTimeout 00:00:05 Maximum time to wait for more log lines, as hh:mm:ss.fff.
retriesMaxAttempts 3 Maximum number of attempts to connect to Logz.io.
retriesInterval 00:00:02 Time to wait between retries, as hh:mm:ss.fff.
debug false To print debug messsages to the console and trace log, true. Otherwise, false.
Code sample
using System.IO;
using log4net;
using log4net.Config;
using System.Reflection;

namespace dotnet_log4net
{
  class Program
  {
    static void Main(string[] args)
    {
      var logger = LogManager.GetLogger(typeof(Program));
      var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

      // Replace "App.config" with the config file that holds your log4net configuration
      XmlConfigurator.Configure(logRepository, new FileInfo("App.config"));

      logger.Info("Now I don't blame him 'cause he run and hid");
      logger.Info("But the meanest thing he ever did");
      logger.Info("Before he left was he went and named me Sue");

      LogManager.Shutdown();
    }
  }
}

Custom fields

You can add static keys and values to be added to all log messages. These custom fields must be children of <appender>, as shown here.

<appender name="LogzioAppender" type="Logzio.DotNet.Log4net.LogzioAppender, Logzio.DotNet.Log4net">
  <customField>
    <key>Environment</key>
    <value>Production</value>
  <customField>
  <customField>
    <key>Location</key>
    <value>New Jerseay B1</value>
  </customField>
</appender>

Extending the appender

To change or add fields to your logs, inherit the appender and override the ExtendValues method.

public class MyAppLogzioAppender : LogzioAppender
{
  protected override void ExtendValues(LoggingEvent loggingEvent, Dictionary<string, string> values)
  {
    values["logger"] = "MyPrefix." + values["logger"];
    values["myAppClientId"] = new ClientIdProvider().Get();
  }
}

Change your configuration to use your new appender name. For the example above, you’d use MyAppLogzioAppender.

Configure NLog

Before you begin, you’ll need: NLog 4.5.0 or higher

Add the dependency to your project

If you’re on Windows, navigate to your project’s folder in the command line, and run this command to install the dependency.

Install-Package Logzio.DotNet.NLog

If you’re on a Mac or Linux machine, you can install the package using Visual Studio. Select Project > Add NuGet Packages..., and then search for Logzio.DotNet.NLog.

Configure the appender

You can configure the appender in a configuration file or directly in the code. Use the samples in the code blocks below as a starting point, and replace them with a configuration that matches your needs.

For a complete list of options, see the configuration parameters below the code blocks.👇

See the NLog documentation for more information on the NLog configuration file.

Option 1: In a configuration file

<nlog>
  <extensions>
    <add assembly="Logzio.DotNet.NLog"/>
  </extensions>
  <targets>

    <!-- Replace these parameters with your configuration -->
    <target name="logzio" type="Logzio"
      token="<<SHIPPING-TOKEN>>"
      logzioType="nlog"
      listenerUrl="<<LISTENER-HOST>>:8071"
      bufferSize="100"
      bufferTimeout="00:00:05"
      retriesMaxAttempts="3"
      retriesInterval="00:00:02"
      debug="false">

      <contextproperty name="host" layout="${machinename}" />
      <contextproperty name="threadid" layout="${threadid}" />
    </target>
  </targets>
  <rules>
      <logger name="*" minlevel="Info" writeTo="logzio" />
  </rules>
</nlog>

Option 2: In the code

var config = new LoggingConfiguration();

// Replace these parameters with your configuration
var logzioTarget = new LogzioTarget {
  Token = "<<SHIPPING-TOKEN>>",
  LogzioType = "nlog",
  ListenerUrl = "<<LISTENER-HOST>>:8071",
  BufferSize = "100",
  BufferTimeout = "00:00:05",
  RetriesMaxAttempts = "3",
  RetriesInterval = "00:00:02",
  Debug = "false",
};

config.AddTarget("Logzio", logzioTarget);
config.AddRule(LogLevel.Debug, LogLevel.Fatal, "Logzio", "*");
LogManager.Configuration = config;
Parameters
Parameter Description
token Your Logz.io account token.
Replace <<SHIPPING-TOKEN>> with the token of the account you want to ship to.
listenerUrl https://listener.logz.io:8071 Listener URL and port.
Replace <<LISTENER-HOST>> with your region’s listener host (for example, listener.logz.io). For more information on finding your account’s region, see Account region.
logzioType nlog The log type, shipped as type field. Used by Logz.io for consistent parsing. Can’t contain spaces.
bufferSize 100 Maximum number of messages the logger will accumulate before sending them all as a bulk.
bufferTimeout 00:00:05 Maximum time to wait for more log lines, as hh:mm:ss.fff.
retriesMaxAttempts 3 Maximum number of attempts to connect to Logz.io.
retriesInterval 00:00:02 Time to wait between retries, as hh:mm:ss.fff.
debug false To print debug messsages to the console and trace log, true. Otherwise, false.
Code sample
using System;
using System.IO;
using Logzio.DotNet.NLog;
using NLog;
using NLog.Config;
using NLog.Fluent;

namespace LogzioNLogSampleApplication
{
  public class Program
  {
    static void Main(string[] args)
    {
      var logger = LogManager.GetCurrentClassLogger();

      logger.Info()
        .Message("If you'll be my bodyguard")
        .Property("iCanBe", "your long lost pal")
        .Property("iCanCallYou", "Betty, and Betty when you call me")
        .Property("youCanCallMe", "Al")
        .Write();

      LogManager.Shutdown();
    }
  }
}

Context properties

You can configure the target to include your own custom values when forwarding to Logz.io. For example:

<nlog>
  <variable name="site" value="New Zealand" />
  <variable name="rings" value="one" />
  <target name="logzio" type="Logzio" token="<<SHIPPING-TOKEN>>">
    <contextproperty name="site" layout="${site}" />
    <contextproperty name="rings" layout="${rings}" />
  </target>
</nlog>

Extending the appender

To change or add fields to your logs, inherit the appender and override the ExtendValues method.

[Target("MyAppLogzio")]
public class MyAppLogzioTarget : LogzioTarget
{
  protected override void ExtendValues(LogEventInfo logEvent, Dictionary<string, string> values)
  {
    values["logger"] = "MyPrefix." + values["logger"];
    values["myAppClientId"] = new ClientIdProvider().Get();
  }
}

Change your configuration to use your new target. For the example above, you’d use MyAppLogzio.