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);
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.
See alsoPrimary 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;
}
}
ILog log = LogManager.GetLogger(this.GetType());
log.DebugFormat("Hi {0}", "dude");
<configuration>
<configSections>
<sectionGroup key="common">
<section key="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>
<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>