Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#
Article

Slogger - The simple extensible logger

Rate me:
Please Sign up or sign in to vote.
3.17/5 (6 votes)
22 Dec 20033 min read 47.8K   899   16   1
An article and library describing simple application logging and instrumentation

Image 1

Introduction

This article introduces a small and simple library to help logging and instrumentation in your applications. Having recently reviewed the area of logging and instrumentation, I discovered some very complete libraries, such as the Enterprise Instrumention Framework components and the Microsoft Logging Block. Although these libraries were vast and feature complete, they were overkill for the relativly simple logging that I needed. I created the Slogger library for my own use and have put it here as it may be useful to others.

Using the code

The Slogger is a two part library containg the core library, and a library containing the Sink objects. The supplied solution also includes a sample application that demonstrates writing Errors and Events to the Event Log. To run the sample application, simply build the supplied solution with Visual Studio .NET 2003.

The library is driven by an XML file that describes two major parts, Events and Sinks. Events are categorised er, events. To give an example, an event can be called ApplicationError or perhaps just Error or Information. Sinks are where an event is consumed. Examples of Sinks could be where the information in an event is written to the disk or event log.

Event are are linked to one or more Sinks. In the sample file, we have an event named Error. This event is wired to two Sinks, EventLogInformation and FileLog. This means that when an Error event is generated, it will be consumed by the EventLogInformation and FileLog sinks which both in turn do something with the event. Here is an excerpt from the sample file that describes an event and how it is linked to two sinks: <

XML
<Event Type="Information">
    <Sink Name="EventLogInformation" />
    <Sink Name="FileLog" />
 </Event>

Below is an excerpt from the sample file that describes the sinks:

XML
<Sink Type="EventLogInformation">
  <Class Name="Slogger.Sinks.EventLogSink, Slogger.Sinks">
    <InitialiserParameter Index="0" Name="EventLogSourceName">
         Slogger Samples</InitialiserParameter>
    <InitialiserParameter Index="1" Name="EventLogEntryTypeEnumeration">
         Information</InitialiserParameter>
  </Class>
</Sink>

The Sink described above is called EventLogInformation and has a class name of EventLogSink in the Slogger.Sinks assembly. It takes two paramaters that are passed to the Initialise method of the class. Below is the sample code that initialises the library with the settings file and fires two events.

C#
Trace.Listeners.Add( new SloggerListener( 
    @"..\..\samples\cs\EventLogAndFileLog\settings.xml"  ) ); 

That's it. The library is now initialized. Of course, there's a few house-keeping things that must be done, like adding a reference to the System.Diagnostics namespace and setting up event log sources:

C#
using System.Diagnostics ;
      if ( !EventLog.SourceExists("Slogger Samples" )) 
        EventLog.CreateEventSource( "Slogger Samples", "Application") ;

Run the sample application. It puts two entries in the event log. Not much of a sample, but remember apart from house-keeping code, the entries got the event log with just two actual lines of code and a bunch of settings in an XML file.

The Slogger library is extensible. It can load your custom Sink classes and pass event information to them. Below is the interface that your custom classes must implement in order for the Slogger library to be able to load an initialize them:

C#
public interface ISink
{
  void Initialise( SinkManager sinkManager, params object[] arguments) ;
  void Write( string message ) ;
  void WriteLine( string message ) ;
}

Each of your classes must implement the above methods. The Write and WriteLine have been implemented. Other overrides from the

Trace 
class have not yet been implemented, but should be simple to add should you need them. The Slogger library will create your class and pass any constructor parameters (if any are specified in the XML). Once constructed, it will call Initialize on your class, again, with any parameters you may have specified in the XML. The XML below is used in the sample application. This XML describes the FileLog sink and specifies the parameters to pass to the constructor of the FileLogSink class:

XML
<Sink Type="FileLog">
  <Class Name="Slogger.Sinks.FileLogSink, Slogger.Sinks">
    <ConstructorParameter Index="0" Name="Filename">
          c:\out.txt</ConstructorParameter >
  </Class>
</Sink>

Points of Interest

Whilst this library is only small (and simple), it does solve a problem many will face. I learnt that the Microsoft Logging Block is reliant on EIF and Web Services Enhancements 2.0 Technology Preview. This library is ideal for those who want simple and light logging without the overhead of involved set-up procedures and reliance (if only a build reliance) on technology preview code.

History

This is the first release of this library. I may update the core

ISink 
interface in the future to support all of the overrides available to the Trace.WriteLine method. It would be nice for those who create their own custom Sink classes to share them with the community.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I was born at a very young age. My hobbies include reading pornography and killing squirrels.

Comments and Discussions

 
Generallog4cpp Pin
Uwe Keim23-Dec-03 4:46
sitebuilderUwe Keim23-Dec-03 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.