Primary purpose of this method is to allow us to parse and load configuration sections using the same API regardless of the .NET framework version.
ILog log = LogManager.GetLogger(this.GetType());
...
try
{
/* .... */
}
catch(Exception ex)
{
log.ErrorFormat("Hi {0}", ex, "dude");
}
The example below shows programmatic configuration of the underlying log system:
// create properties
NameValueCollection properties = new NameValueCollection();
properties["showDateTime"] = "true";
// set Adapter
Common.Logging.LogManager.Adapter = new
Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter(properties);
public class ConsoleOutLogger : AbstractSimpleLogger
{
public ConsoleOutLogger(string logName, LogLevel logLevel, bool showLevel, bool showDateTime,
bool showLogName, string dateTimeFormat)
: base(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
{
}
protected override void WriteInternal(LogLevel level, object message, Exception e)
{
// Use a StringBuilder for better performance
StringBuilder sb = new StringBuilder();
FormatOutput(sb, level, message, e);
// Print to the appropriate destination
Console.Out.WriteLine(sb.ToString());
}
}
public class ConsoleOutLoggerFactoryAdapter : AbstractSimpleLoggerFactoryAdapter
{
public ConsoleOutLoggerFactoryAdapter(NameValueCollection properties)
: base(properties)
{ }
protected override ILog CreateLogger(string key, LogLevel level, bool showLevel, bool
showDateTime, bool showLogName, string dateTimeFormat)
{
ILog log = new ConsoleOutLogger(key, level, showLevel, showDateTime, showLogName,
dateTimeFormat);
return log;
}
}
// configure for capturing
CapturingLoggerFactoryAdapter adapter = new CapturingLoggerFactoryAdapter();
LogManager.Adapter = adapter;
// reset capture state
adapter.Clear();
// log something
ILog log = LogManager.GetCurrentClassLogger();
log.DebugFormat("Current Time:{0}", DateTime.Now);
// check logged data
Assert.AreEqual(1, adapter.LoggerEvents.Count);
Assert.AreEqual(LogLevel.Debug, adapter.LastEvent.Level);
<configuration>
<configSections>
<sectionGroup key="common">
<section key="logging"
type="Common.Logging.ConfigurationSectionHandler, Common.Logging"
requirePermission="false" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.DebugLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="ALL" />
</factoryAdapter>
</logging>
</common>
</configuration>
<configuration>
<configSections>
<sectionGroup key="common">
<section key="logging"
type="Common.Logging.ConfigurationSectionHandler, Common.Logging"
requirePermission="false" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.NoOpLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="ALL" />
</factoryAdapter>
</logging>
</common>
</configuration>
<system.diagnostics>
<sharedListeners>
<add name="Diagnostics"
type="Common.Logging.Simple.CommonLoggingTraceListener, Common.Logging"
initializeData="DefaultTraceEventType=Information; LoggerNameFormat={listenerName}.{sourceName}">
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Information"/>
</add>
</sharedListeners>
<trace>
<listeners>
<add name="Diagnostics" />
</listeners>
</trace>
</system.diagnostics>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="showLogName" value="true" />
<arg key="showDateTime" value="true" />
<arg key="level" value="ALL" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
</configuration>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging"
type="Common.Logging.ConfigurationSectionHandler, Common.Logging"
requirePermission="false" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="ALL" />
</factoryAdapter>
</logging>
</common>
</configuration>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging"
type="Common.Logging.ConfigurationSectionHandler, Common.Logging"
requirePermission="false" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="ALL" />
</factoryAdapter>
</logging>
</common>
</configuration>