log4net logging

Intro

log4net is “a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® .NET runtime.”

Papertrail supports aggregating messages from a native log4net appender, providing a live searchable log console for your .NET app.

If log4net isn’t already in use and a simpler solution is preferable, use SimpleSysLog instead.

Installation

Install log4net

Install log4net in your app. At its core, this is importing the classes:

using log4net;
using log4net.Config;

and then initializing log4net by pointing it to a config file path (which we’ll create in the next step):

XmlConfigurator.Configure(new System.IO.FileInfo(args[0]));

In the example above, the path to the config file is in args[0]. Replace that variable with a variable set by your app’s runtime configuration or a path or UNC path as a string (more).

Here’s full log4net docs.

Create log4net config file

Here’s a sample log4net config file with 1 logger (the top-level root logger) sending only to 1 appender. That RemoteSyslogAppender instance sends to Papertrail.

Logs from this app will be identified with the system’s hostname and the app name MyApp, as defined in the config Identity option below. See Tweak Identity and PatternLayout for ways to control this.

Important: In the example below, replace logsN.papertrailapp.com and XXXXX with the details from Papertrail’s Add Systems page.

<log4net>
  <appender name="PapertrailRemoteSyslogAppender" type="log4net.Appender.RemoteSyslogAppender">
    <facility value="Local6" />
    <identity value="%date{yyyy-MM-ddTHH:mm:ss.ffffffzzz} %P{log4net:HostName} MyApp" />
    <layout type="log4net.Layout.PatternLayout" value="%level - %message%newline" />
    <remoteAddress value="logsN.papertrailapp.com" />
    <remotePort value="XXXXX" />
  </appender>

  <root>
    <level value="DEBUG" />
    <appender-ref ref="PapertrailRemoteSyslogAppender" />
  </root>
</log4net>
</code>

Tweak Identity and PatternLayout

In the configuration above, the log message is defined with:

<identity value="%date{yyyy-MM-ddTHH:mm:ss.ffffffzzz} %P{log4net:HostName} MyApp" />
<layout type="log4net.Layout.PatternLayout" value="%level - %message%newline" />

This will generate a message like:

Mar 23 23:59:59 SYSTEM_NAME MyApp: INFO - the message

Both the identity and the format can be customized. The identity could include the name of your app and/or a fixed hostname.

The PatternLayout can include additional values, which are covered in log4net’s PatternLayout documentation. file, logger, and %aspnet-request{key} may be relevant to your application.

Attributes

Consider using ThreadContext to set arbitrary keys and values, such as a user ID or request ID. For example:

ThreadContext.Properties["user"] = userName;
ThreadContext.Properties["request"] = "abc123";

Use

Papertrail is just another log4net target, so no code changes should be needed for apps which already use log4net.

If you aren’t yet using log4net, generate messages with:

private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
log.Debug("Did it again!");

See log4net docs.