Documentation forPapertrail

C# and .NET (log4net)

Introduction

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 in your app. At its core, this is importing the classes:

Copy
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):

Copy
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.

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

Copy
<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>

Tweak Identity and PatternLayout

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

Copy
<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:

Copy
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:

Copy
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:

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

See log4net docs.

The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.