DEV Community

Cover image for NLog vs log4net
Thomas Ardal
Thomas Ardal

Posted on • Originally published at blog.elmah.io

NLog vs log4net

Both NLog and log4net are two very popular choices when selecting a logging framework for .NET applications. In this post, you will learn all about both frameworks and the differences. For those of you who don't know one or both of the frameworks, let's start with a few words about each framework.

log4net

log4net

Probably the oldest logging framework on the block, log4net has existed pretty much since .NET was introduced. log4net was originally an internal Apache log4j port developed by Neoworks Limited back in 2001. The project quickly moved to Sourceforge (the GitHub of 2001) and was released under the Apache license. Since that time, log4net has been one of the most popular choices in the .NET world for adding logging to applications.

log4net works with the concept of appenders, where log messages can be routed to different data stores. A lot of appenders have been implemented during the years, like logging to the file system, SQL Server, HTTP endpoints, and even NoSQL databases, however being unstructured text messages, log4net and NoSQL don’t exactly go hand in hand.

NLog

NLog

While log4net quickly became the default choice, alternatives began to show up. Probably the first real competitor to log4net's dominance was NLog. Originally developed by Jarek Kowalski and with pull requests from almost 100 people, NLog is a great alternative. While log4net pretty much stood still from 2006, NLog just kept going. While Jarek seemed to pull the plug when starting at Google, the community seemed to step up and new releases are still flowing.

Like log4net, NLog contains multiple log targets and is able to log messages to various data stores.

Configuration

All logging frameworks needs some kind of configuration, in order for the framework to know what to log and to where.

log4net

log4net is configured through your app.config file or a dedicated log4net.config:

<log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="log-file.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %message%newline" />
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="FileAppender" />
  </root>
</log4net>

NLog

NLog can be configured through both XML and C#. Most people use XML, since that has been there from the beginning and since the C# based API isn't as nice as newer frameworks like Serilog.

<nlog>
  <targets>
    <target xsi:type="File" name="file" fileName="log-file.log"
            layout="${longdate} ${level} ${message}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />
  </rules>
</nlog>

Logging Messages

When it comes to logging messages from your application, log4net and NLog works pretty similar.

log4net

Log messages are written using the log4net API:

var log = LogManager.GetLogger(typeof(Bar));
log.Debug("Hello World");

Additional log information can be attached to a log message by using a range of different contextual options:

log4net.GlobalContext.Properties["Application"] = "MyCoolApp";
using(log4net.ThreadContext.Stacks["NDC"].Push("context"))
{
    log.Debug("Hello World");
}

While you can embed information directly in log messages with log4net, semantic logging isn’t supported.

NLog

NLog provides an API similar to log4net for logging messages to the configured set of targets:

var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Debug("Hello World");

Additional properties can be attached as well:

var msg = new LogEventInfo(LogLevel.Debug, "", "Hello World");
msg.Properties.Add("Application", "MyCoolApp");
logger.Debug(msg);

The recent version of NLog supports semantic logging. To add semantic to your log messages, use the curly brace syntax:

logger.Info("Order {orderid} created for {user}", 42, "Kenny");

Conclusion

Picking a winner isn't the goal of this post. Both logging frameworks has its own benefits and drawbacks.

log4net

Advantages

  • Widely adopted
  • A lot of documentation and resources online

Disadvantages

  • Doesn't use new possibilities in .NET
  • No structured logging

NLog

Advantages

  • Structured logging
  • More modern API
  • Better support for NoSQL databases

Disadvantages

  • Not as many resources as log4net

Would your users appreciate fewer errors?

elmah.io is the easy error logging and uptime monitoring service for .NET. Take back control of your errors with support for all .NET web and logging frameworks.

➡️ Error Monitoring for .NET Web Applications ⬅️

This article first appeared on the elmah.io blog at https://blog.elmah.io/nlog-vs-log4net/

Top comments (0)