Log.Debug( m=>m("result is {0}", random.NextDouble()) );
Log.Debug(delegate(m) { m("result is {0}", random.NextDouble()); });
// 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);
<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>
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.
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.
<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="showDataTime" value="true" />
<arg key="level" value="ALL" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
</configuration>
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);
ILog log = LogManager.GetLogger(this.GetType());
log.DebugFormat("Hi {0}", "dude");
<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="level" value="DEBUG" />
</factoryAdapter>
</logging>
</common>
</configuration>
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 name, LogLevel level, bool showLevel, bool
showDateTime, bool showLogName, string dateTimeFormat)
{
ILog log = new ConsoleOutLogger(name, level, showLevel, showDateTime, showLogName,
dateTimeFormat);
return log;
}
}
<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.NoOpLoggerFactoryAdapter, 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>