65.9K
CodeProject is changing. Read more.
Home

EZLogger - drop-dead easy logging

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.65/5 (18 votes)

Dec 4, 2006

CPOL

2 min read

viewsIcon

86107

downloadIcon

1794

A lightweight .NET logging component.

Introduction

EZLogger is an object that provides basic logging facilities to your application. EZLogger is a lightweight alternative to more feature-rich logging subsystems such as Log4Net and XQuiSoft Logging, both of which are excellent logging mechanisms. I wrote EZLogger because my application's logging needs were simple and I wanted to keep my app's footprint small. I hope you find EZLogger useful and welcome your suggestions.

Features

EZLogger writes log messages to a text file. The log file can be appended to, or a new one created when EZLogger is initialized. Log messages include a timestamp, severity and message text. Logging can be paused and resumed at any time. EZLogger's logging level (i.e. severities of interest) can be changed at run time to filter out unwanted messages. EZLogger is thread safe and can be used to log the activity of multiple threads.

EZLogger supports any combination of these severity levels:
  • Debug - trace and debug messages
  • Info - informational messages
  • Success - success messages
  • Warning - warnings
  • Error - error messages
  • Fatal - fatal errors
  • All - all messages

How to use EZLogger

You use EZLogger by creating a new instance, starting the logger, and calling one of the various logging methods. When you're done using the logger, you stop it as shown below.
  // Create and start the logger
  uint logLevels = (uint) EZLogger.Level.All;
  EZLogger logger =
    new EZLogger ("C:\\EZLoggerTester.log",  // log filename
                  false,                     // don't append
                  logLevels);                // log levels of interest
  bool bStatus = logger.Start();

  // Write log messages
  logger.Info ("An informational message");
  logger.Warning ("A warning");
  logger.Fatal ("A fatal error has occured");
  ...

  // Stop logging
  logger.Stop();

This code fragment produces the following log file.

12/4/2006 4:38:26 PM  I: An informational message
12/4/2006 4:38:26 PM  W: A warning
12/4/2006 4:38:26 PM  F: A fatal error has occur

The Pause() and Resume() methods can be used to temporarily suspend and restart logging.

  // Starting long, boring operation
  logger.Pause();
  performLongBoringOperation();
  logger.Resume();

The Levels property can be set to restrict logging to message severities of interest.

  // Only interested in errors and debug msgs
  logger.Levels = (uint) (EZLogger.Level.Error | EZLogger.Level.Debug);
  logger.Info ("...");   // won't be logged
  logger.Error ("...");  // will be logged

How it works

Starting EZLogger causes it to maintain a reference to a StreamWriter. Calls to log messages invoke StreamWriter.WriteLine() which causes formatted text to be written (and flushed) to the log file. Messages are filtered based on the object's LogLevel property. EZLogger's methods are synchronized by marking their bodies as critical sections (using the lock keyword).

Revision History

  • 22 Mar 2007
    Added GetMessageCount() method to retrieve the number of messages logged. Thanks to John Tibbits for the suggestion!
  • 5 Dec 2006
    Added explicit Resume() method instead of overloading the behavior of Start().
    Added ability to filter on arbitrary severity levels.
  • 4 Dec 2006
    Initial version.