Logger Class
Class that provides common logging properties and methods.
Namespace: Logging.HelperAssembly: Logging.Helper (in Logging.Helper.dll) Version: 2.0.6
public sealed class Logger
- Inheritance
- Object Logger
These are the different types of log message that can be used:
Type | Description |
---|
Fatal | Use to log non-recoverable exceptions that
unexpectedly end the process. |
Error | Use to log exceptions that have been handled but caused
the processing to stop. |
Warning | Use to indicate that the process faced a potential problem
but can continue. |
Information | These are purely informational messages; they should not
be used to indicate a fault or error state. |
Debug | Used to indicate that the logged message can be used for
debugging purposes to help find the solution to tricky bugs. |
In the program that requires logging functionality:
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:
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 );
}
}
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.
|
ConfigFile | Gets the logging configuration file name. |
ErrorCount | Gets the count of error messages logged. |
FatalCount | Gets the count of fatal messages logged. |
HasErrors | Indicates whether errors exists. |
MaxLogFiles | Gets or sets the maximum number of log files to keep. |
WarnCount | Gets the count of warning messages logged. |
cExtension | Log file extension (suffix) including the period. |