Logging is one of those “system” components that always seems to either be left out or way over-engineered (glares at Microsoft’s Enterprise Application Blocks). Today, I’d like to introduce you to a logging framework that’s everything it needs to be and nothing it doesn’t.
The .Net Logging Framework from The Object Guy is powerful enough to handle any of your logging needs, but simple and painless to use. Here’s a relatively complicated example — we’re going to log to three logging sources to demonstrate how easy it is to set up. In most cases, of course, you’ll log to only one or two sources:
/* first instantiate some basic loggers */ Logger consoleLogger = TextWriterLogger.NewConsoleLogger(); Logger fileLogger = new FileLogger("unit_test_results.log"); Logger socketLogger = new SerialSocketLogger("localhost", 12345); /* now instantiate a CompositeLogger */ logger = new CompositeLogger(); /* add the basic loggers to the CompositeLogger */ logger.AddLogger("console", consoleLogger); logger.AddLogger("file", fileLogger); logger.AddLogger("socket", socketLogger); /* now all logs to logger will automatically be sent to the contained loggers as well */ /* logging is a one-liner */ logger.LogDebug("Logging initialized.");
When you download this logger, you’ll get all the source code, including a socket reader to catch the logs thrown by the socketLogger in the example above. Extending the logger is a piece of cake, too, so you could build yourself a WCF Logger, for instance, in no time flat.
You’ll note the lack of config file-driven settings in the example above — this is purely intentional. You can decide if you want to make any of these settings configurable, and do so in the format you’re comfortable with, so you don’t need to try to get your config files to conform to whatever format your logger insists on using. This small simplification can be a big time-saver for simple apps, debugging / test harness apps, and so on.