Logger using log4net or custom logger.
// Logger helper class.
class Sample {
ILogger log = Log.OfType<Sample>();
// or
ILogger log = Log.Default; // calls Log.OfType<Log4NetLogger>() or configured logger
}
// Log4NetLogger class configures log4net. It also watches the log4net config file for changes at runtime.
XmlConfigurator.ConfigureAndWatch(configFile);
// Separate log4net.config file.
<log4net configSource="AppConfig\log4net.config" />
// log4net file appenders:
"default-rolling": 10 files, 2MB each
"rolling-by-date": files per date, 10 files, 2MB each
"per-execution-rolling": file per program execution, 10 files max
"smtp": email immediately when fatal event occurs (no events buffered)
"filter-by-logger": filter logs by matching logger name
// The log declaration is still cumbersome as type has to specified.
class Foo {
// logger: rm.LoggingTestConsole.Foo
private static readonly ILog log = Log.OfType<Foo>();
}
class Bar {
// logger: rm.LoggingTestConsole.Bar
private static readonly ILog log = Log.OfType<Bar>();
}
class Baz {
// logger: rm.LoggingTestConsole.Baz
private static readonly ILog log =
Log.OfType(MethodBase.GetCurrentMethod().DeclaringType);
}
class Qux {
// logger: rm.Logging.Log4NetLogger
private static readonly ILog log = Log.Default; // calls Log.OfType<Log4NetLogger>() or configured logger
}
// Log helper class initializes the logger with the Log type and wraps log4net methods.
class Xyzzy {
private void Debug() {
// logger: rm.Logging.Log4NetLogger
Log.Debug("some message"); // calls Log.OfType<Log4NetLogger>() or configured logger
}
}
// Configure different logger if needed.
class MyLogger : ILogger
{
// todo: implement ILogger methods
}
// Add key in web/app config for logger's type name.
<add key="logger" value="dev.Logging.MyLogger"/>
Console project:
- Look at
rm.LoggingTestConsole
project for configuration. Add theAppConfig\log4net.config
file to your project and below inapp.config
. - Include/Exclude the appenders in
log4net.config
.
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net configSource="AppConfig\log4net.config" />
</configuration>
Web, MVC project:
- Look at
rm.LoggingTestWeb
orrm.LoggingTestMvc
project for configuration. Add theAppConfig\log4net.config
file to your project and below inweb.config
. - Include/Exclude the appenders in
log4net.config
.
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net configSource="AppConfig\log4net.config" />
</configuration>