Logger Class

Class that provides common logging properties and methods.

Definition

Namespace: Logging.Helper
Assembly: Logging.Helper (in Logging.Helper.dll) Version: 2.0.6
C#
public sealed class Logger
Inheritance
Object    Logger

Remarks

These are the different types of log message that can be used:

TypeDescription
FatalUse to log non-recoverable exceptions that unexpectedly end the process.
ErrorUse to log exceptions that have been handled but caused the processing to stop.
WarningUse to indicate that the process faced a potential problem but can continue.
InformationThese are purely informational messages; they should not be used to indicate a fault or error state.
DebugUsed to indicate that the logged message can be used for debugging purposes to help find the solution to tricky bugs.

Example

In the program that requires logging functionality:
C#
using Logging.Helper;
public class Program
{
  /// Public static Logger.
  // Logging configuration in the application settings (App.config)...
  public static readonly Logger Logging = new Logger( typeof( Program ) );

  // ... OR shared logging configuration filename specified in the application settings...
  public static readonly Logger Logging = new Logger( Properties.Settings.Default.LogConfig );

  // ... OR shared logging configuration filename hard-coded in each program...
  public static readonly Logger Logging = new Logger( MethodBase.GetCurrentMethod().DeclaringType, "Sample.log4net" );

  static void Main( string[] args )
  {
    try
    {
      Logging.Log( "Test specified message type.", LogSeverity.Warning );
    }
    catch( Exception ex )
    {
      // Handle any unexpected exceptions
      Logging.Fatal( ex.ToString() );
    }
    finally
    {
      // Set the program exit code based on whether any errors have been logged
      Environment.ExitCode = Logging.HasErrors ? 1 : 0;
    }
    Environment.Exit( Environment.ExitCode );
  }
}

To allow a processing class to log using the logger created in the program it must inherit from the Logging.Helper.LoggerEvent class and have the event handler set after it has been instantiated:
C#
using Logging.Helper;
public class ProcessingClass : LoggerEvent
{
  internal int DoProcessing()
  {
    var retValue = 0; // Assume normal completion
    try
    {
      // This method is inherited from the LoggerEvent class
      RaiseLogEvent( "Information message about the processing..." );
    }
    catch( Exception ex )
    {
      RaiseLogEvent( ex.ToString(), LogSeverity.Fatal );
      retValue = 1; // Abnormal completion
    }
    return retValue;
  }
}

using Logging.Helper;
public class Program
{
  /// Public static Logger.
  public static readonly Logger Logging = new Logger( typeof( Program ) );

  static void Main( string[] args )
  {
    try
    {
      // Create an object from a class that inherits LoggerEvent
      var logicClass = new ProcessingClass();
      logicClass.RaiseLogHandler += Logging.OnRaiseLog;

      // Do Processing and set the Exit code
      logicClass.DoProcessing();
    }
    catch( Exception ex )
    {
      // Handle any unexpected exceptions
      Logging.Fatal( ex.ToString() );
    }
    finally
    {
      // Set the program exit code based on whether any errors have been logged
      Environment.ExitCode = Logging.HasErrors ? 1 : 0;
    }
    Environment.Exit( Environment.ExitCode );
  }
}

Constructors

Logger(String) Initializes a new instance of the Logger class using an optional configuration file name.
Logger(Type, String) Initializes a new instance of the Logger class using a logger type and configuration file name.

Properties

ConfigFileGets the logging configuration file name.
ErrorCountGets the count of error messages logged.
FatalCountGets the count of fatal messages logged.
HasErrorsIndicates whether errors exists.
MaxLogFilesGets or sets the maximum number of log files to keep.
WarnCountGets the count of warning messages logged.

Methods

Debug(String)Logs a debugging message.
Debug(String, Object)Logs a debugging message with arguments.
Error(Exception)Logs an exception.
Error(String)Logs an error message.
Error(String, Exception)Logs an error message with an exception.
Error(String, Object)Logs an error message with arguments.
Fatal(Exception)Logs a fatal exception.
Fatal(String)Logs a fatal error message.
Fatal(String, Object)Logs a fatal error message with arguments.
Info(String)Logs an informational message.
Info(String, Object)Logs an informational message with arguments.
LogLog a message.
OnRaiseLogEvent handler for the logging process.
RemoveLogsRemoves the oldest non-read-only log files in a directory.
SetLogFileSets the location of the log file and optionally, the log file name.
ToStringConverts the value of this instance to a string.
(Overrides ObjectToString)
Warn(String)Logs a warning message.
Warn(String, Object)Logs a warning message with arguments.

Fields

cExtensionLog file extension (suffix) including the period.

See Also