Image may be NSFW.
Clik here to view.Earlier this week Scott Hanselman tweeted a link to the MS Live Labs Simple Logging Framework (SLF). I thought I'd check it out and post some comparisons to log4net for posterity. Note this isn't a performance comparison, just a look at how the frameworks are used.
Spoiler: There are some neat ideas in here. Check out SLF if you're new to logging, but my guess is that you'll quickly outgrow its capabilities. If you're already logging, I don't see any reason to switch from more mature frameworks anytime soon.
A Quick Comparison
SLF is remarkably similar to log4net in its design. SLF uses the concepts of appenders (SLF calls them "logs"), loggers (SLF calls them "loggers" too), and logging levels just like log4net. In fact, there was virtually no change when porting my "hello world" log4net example to the SLF:
using System; using LiveLabs.Logging; namespace SimpleLoggingFrameworkExample { class Program { static void Main( string[] args ) { Logger Log = new Logger( typeof( Program ) ); Log.Level = LogLevel.Debug; Log.Debug( "Hello World!" ); Log.Inform( "I'm a simple log4net tutorial." ); Log.Warn( "... better be careful ..." ); Log.Error( "ruh-roh: an error occurred" ); Log.Fatal( "OMG we're dooooooomed!" ); Console.ReadLine(); } } }
The most annoying change (and the cause of a failed build) was the rename of log4net's ILog.Info method to SLF's Logger.Inform. Seems silly to me to change that one method name when the rest of the interface is identical....
Simple Formatting Control
SLF defaults to outputting to the process STDERR stream at a log level of Inform. Line 10 drops the log level to Debug so we can see all of the output, which is formatted rather heinously IMO:
2009-03-06T20:49:57| |No Thread Name|SimpleLoggingFrameworkExample.Program|Hello World! 2009-03-06T20:49:57| |No Thread Name|SimpleLoggingFrameworkExample.Program|I'm a simple log4net tutorial. 2009-03-06T20:49:57| |No Thread Name|SimpleLoggingFrameworkExample.Program|...better be careful ... 2009-03-06T20:49:57| |No Thread Name|SimpleLoggingFrameworkExample.Program|ruh-roh: an error occurred 2009-03-06T20:49:57| |No Thread Name|SimpleLoggingFrameworkExample.Program|OMG we're dooooooomed!
Fortunately SLF provides a straightforward way to alter the message format. Every log object has a MessageFormatter property that can be set to a delegate to format messages for the log. Adding a bit of code to the example:
Log.Level = LogLevel.Debug; Log.MessageFormatter = ( msg, log, level ) => String.Format( "{0} [{1}]: {2}", log, level, msg ); Log.Debug( "Hello World!" );makes the output far more readable:
SimpleLoggingFrameworkExample.Program [Debug]: Hello World! SimpleLoggingFrameworkExample.Program [Inform]: I'm a simple log4net tutorial. SimpleLoggingFrameworkExample.Program [Warn]: ... better be careful ... SimpleLoggingFrameworkExample.Program [Error]: ruh-roh: an error occurred SimpleLoggingFrameworkExample.Program [Fatal]: OMG we're dooooooomed!
A similar callback exists to format exception objects. It is certainly easier to achieve this level of formatting control in SLF than it is in log4net, where you have to define custom formatter objects and layout patterns in your configuration.
Configuration
SLF doesn't require an explicit call to initialize it with a configuration the way log4net does. As I've pointed out SLF defaults to logging to STDERR at an Inform logging level.
If you do want some control, SLF does offer a Settings class that provides programmatic access to configure defaults and specifics for any of the loggers you've created:
Settings.Default.DefaultLogLevel = LogLevel.Debug;
However, your options are limited. You can set a default log level and log for all logger objects, and you can register message and exception formatters. One glaring omission from the configuration space is the ability to configure default message and exception formatters for all loggers. Looks like you're stuck setting them individually for now.
Language Feature Support
Another area where SLF improves on log4net is in the use of newer C# language features (log4net is based on the 2.0 version of the language). Every log method in SLF accepts a lamba expression as an argument:
Log.Debug( () => { IDictionary envs = Environment.GetEnvironmentVariables(); return "Current Environment: " + String.Join( ";", ( from string k in envs.Keys select ( k + "=" + envs[ k ].ToString() ) ).ToArray() ); } );
This is an excellent way to isolate complex logging logic, and to avoid expensive calls to obtain log message formatting arguments when no logging will actually take place; e.g.:
Log.Debug( () => String.Format( "Result: [{0}]", obj.AVeryExpensiveOperation() ) );
To achieve the same effect in log4net, you have to explicitly wrap the code in a check of current log level:
if( Log.IsDebugEnabled ) { Log.DebugFormat( "Result: [{0}]", obj.AVeryExpensiveOperation() ); }
Personally, I prefer the former.
Appender Options
This is where log4net beats SLF into a pulp. log4net ships with a cornucopia of appender options, including the console, files, the event log, the debugger, the .NET trace subsystem, MSMQ, remoting, network endpoints, and various databases. SLF ships with basically the equivalent of the log4net ConsoleAppender, FileAppender, and RollingFileAppender.
In addition, SLF offers no built-in decoupling of the log formatting from your logging code the way log4net does. I've shown in this post how easily you can override the message or exception format on a SLF logger object in your code, but consider also that there is no way to change this message format without changing your code. E.g., if you someday decide to move from text to an XML log, you have to touch your code. The log4net formatting configuration can be isolated to the app.config, making it touchable while leaving your code alone.
Summary
To sum up, the major differences between log4net and SLF are:
- log4net offers far more appender options and configurability.
- log4net has a larger user-base and more maturity.
- SLF provides easier access to simple formatting options for messages and exceptions.
- SLF supports the latest language features, while log4net does not.
I'll be honest - I like some of the ideas I see in the SLF project, but I really can't figure out the impetus for a new project when a mature and open project already exists. There is this blurb from the project home page on codeplex:
Existing logging libraries are either too feature-light or too complex. That means you might end up spending as much time debugging your logging code as you do your application code. Or you may not even know that your “log” to database is even working until it’s too late.
Yes, SLF makes a few things easier, but on the whole log4net offers far more for features for the same effort. And I can't say I've encountered these difficulties with log4net - I don't consider log4net complex (in comparison to, say, the MS Enterprise Logging Framework), but that's probably because I've been using log4net for years. So let's say for argument's sake that log4net is overly complex and difficult to use. The question at the front of my mind is this:
Why start over when you can openly contribute to the log4net project and make it better?
There is nothing earth-shaking in SLF that I see mandating a new project effort. And by not contributing to log4net and starting over, you're dismissing the features and maturity it has to offer.
[insert "MS hubris! They don't 'get' the open source thing...." comment here]