-
Notifications
You must be signed in to change notification settings - Fork 2
Usage
ikopylov edited this page Jun 17, 2015
·
10 revisions
- To create logger instance from App.config just call:
var logger = Qoollo.Logger.LoggerFactory.CreateLoggerFromAppConfig("your name", "LoggerConfigurationSection");- To create logger instance from configuration classes in source code use this method:
var logger = Qoollo.Logger.LoggerFactory.CreateLogger("your name", configuration);- To initialize global logger singleton (LoggerDefault) you can use this code:
Qoollo.Logger.LoggerDefault.LoadInstanceFromAppConfig();
// or
Qoollo.Logger.LoggerDefault.SetInstance(<already created logger>);Commonly used methods:
public class SampleClass
{
private static readonly Logger _logger = LoggerStatic.GetThisClassLogger();
public void SampleMethod()
{
try
{
// One can check is required log level enabled
if (_logger.IsTraceEnabled)
{
// Simple trace message
_logger.Trace("Trace message");
}
// Formatted message at Trace level (formatting supported for any LogLevel)
_logger.TraceFormat("Trace message. Some value = {0}", -1);
// Message with context
_logger.Debug("Debug message", "context");
_logger.Info("Info message");
_logger.Warn("Warn message");
_logger.Error("Error message");
// Passing LogLevel as parameter
_logger.Log(LogLevel.Info, "Log message");
}
catch (Exception ex)
{
// Message with attached exception (exception can be attached at any LogLevel)
_logger.Fatal(ex, "Unhandled exception");
throw;
}
}
}In this mode 'Class', 'Namespace' and 'Assembly' parameters added to log messages from current type:
public class SampleClass
{
private static readonly Logger _logger = LoggerDefault.Instance.GetThisClassLogger();
public void SampleMethod()
{
// ..........
}
}It is a good practice to create separate logger singleton in every assembly. This reduces the level of coupling. With our logger you can stack that singletons (each logger will wrap the higher level logger).
Here is the sample of Logger singleton:
/// <summary>
/// Local assembly logger
/// </summary>
internal class Logger : Qoollo.Logger.Logger
{
private static Logger _instance = new Logger(Qoollo.Logger.LogLevel.FullLog, GetEmptyLogger());
/// <summary>
/// Singleton instance
/// </summary>
public static Logger Instance
{
get { return _instance; }
}
/// <summary>
/// Logger initialization method
/// </summary>
/// <param name="innerLogger">Parent logger</param>
[Qoollo.Logger.LoggerWrapperInitializationMethod]
public static void Init(Qoollo.Logger.ILogger innerLogger)
{
_instance = new Logger(innerLogger.Level, innerLogger);
//InitializeLoggerInAssembly(_instance, typeof(<any type in dependent assembly>).Assembly);
}
private Logger(Qoollo.Logger.LogLevel logLevel, Qoollo.Logger.ILogger innerLogger)
: base(logLevel, "Module name", innerLogger)
{
}
}To simplify the initialization of stacked loggers you can use the 'Qoollo.Logger.Initialization.Initializer' class:
Qoollo.Logger.Initialization.Initializer.InitializeLoggerInAssembly(logger, typeof(<any public type in dependent assembly>).Assembly);It scans the supplied assembly for classes inherited from 'Qoollo.Logger.Logger' and call their initialization method (method tagged with 'Qoollo.Logger.LoggerWrapperInitializationMethod' attribute).