Introducing Caliburn.Micro.Logging

In the spirit of continuing to NuGet my extensions, I decided to try and create a NuGet package for log4net and nlog. However, I still found my System.Diagnostics.Debug logger to be useful, and did not want to include it twice. This led me to do a quick refactoring of the logging code from How to do Logging with Caliburn.Micro to create a new assembly Caliburn.Micro.Logging which would include the common code between the log4net and nlog loggers. Since I was already touching code, I decided to go a few steps further and add a Trace logger, and formalize some extensions to the ILog interface.

New Functionality

TraceLogger

One of the most frequent requests I received on my original logging post, was to support System.Diagnostics.Trace. While I don’t use the Trace functionality all that often, I do realize others do, so I decided to add it. The new code is simple cut-and-paste inheritance of the DebugLogger where I replaced Debug with Trace. This is one of those moments I wished that there was a way for classes with static methods could have interfaces to define a contract, perhaps a static interface description. That’s a rant for another post sometime in the future.

ILogExtended

I had long ago created a derived ILog interface and modified my log implementations to support richer error messages. As I was researching how others were logging with Caliburn.Micro, I read the Extending the ILog interface discussion on the Caliburn.Micro CodePlex site. It seemed that others had come to a similar conclusion about extending ILog. So I decided to add it to my Caliburn.Micro.Logging library. Of course, this means that I modified the Debug and Trace loggers to support the new methods.

Here is the code for the ILogExtended interface:

How to Use Caliburn.Micro.Logging

Using NuGet for Caliburn.Micro.Logging

The NuGet package id is Caliburn.Micro.Logging. So you can install the package from the NuGet package manager shell using the following command:

or if you are more GUI minded, use the Add Package Reference dialog.

Getting the Code for Caliburn.Micro.Logging

You can find the code for Caliburn.Micro.Logging on github at https://github.com/dbuksbaum/[Caliburn.Micro.Logging](https://github.com/dbuksbaum/Caliburn.Micro.Logging).

Configuring Your Code to use Caliburn.Micro.Logging

Once you have added the Caliburn.Micro.Logging NuGet package to your project, just modify the bootstrapper by adding a static constructor that sets the GetLog delegate. In this sample I show the DebugLogger, but it could also be the TraceLogger.

static AppBootstrapper()
{
  LogManager.GetLog = type => new DebugLogger(type);
}

Once that change is made, compile and run. That’s it. You will now see debug or trace output from both the Caliburn.Micro framework, and any of your own log statements.

Summary

This is another small change that grew a bit larger than expected. However, the end result is useful in and of itself. It also provides me a clean baseline for log4net and nlog loggers to now be clean well-formed NuGet packages. I hope to add those in the very near future.

In the meantime, if you have suggestions, comments, or critiques, please add a comment below, add an issue to the issue tracker, or send me email using the blog comment form.

Related

comments powered by Disqus