Enterprise Library Logging
Logger code
Log4net handles logging info/ warning/ error type messages simple, but Enterprise Library Logging Application Block isn't so obvious. Here's a simple facade I use. See also my no-configuration log loader.
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace EntLib5Logging
{
/// <summary>
/// Facade for writing log entries in log4net style to EntLib Logging
/// </summary>
public static class Log
{
/*
* We don't use priority or eventId
* We get the calling method from the stackFrame (careful with inlining here)
*/
private static string GetCallingMethod(StackFrame frame)
{
//don't calculate this if we aren't logging
if (!Logger.IsLoggingEnabled()) return string.Empty;
var method = frame.GetMethod();
return method.DeclaringType.FullName + "." + method.Name;
}
/// <summary>
/// Logs an informational message
/// </summary>
/// <param name="message">The message.</param>
public static void Info(string message)
{
var category = GetCallingMethod(new StackFrame(1));
Logger.Write(message, category, 0, 0, TraceEventType.Information);
}
/// <summary>
/// Logs a warning message
/// </summary>
/// <param name="message">The message.</param>
public static void Warn(string message)
{
var category = GetCallingMethod(new StackFrame(1));
Logger.Write(message, category, 0, 0, TraceEventType.Warning);
}
/// <summary>
/// Logs an error message
/// </summary>
/// <param name="message">The message.</param>
public static void Error(string message)
{
var category = GetCallingMethod(new StackFrame(1));
Logger.Write(message, category, 0, 0, TraceEventType.Error);
}
}
}
Configuration
99% of the time I use a simple text log files, and I want to switch between logging everything and logging just errors. This is EntLib 5- EntLib 4 was exactly the same (if you change the EntLib version number to 4.1.0.0). Here's the copy to paste...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Error Trace Listener"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="errors.log"
formatter="Simple Text Formatter"
header=""
footer=""
filter="Error" />
<add name="Full Trace Listener"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="full.log"
rollFileExistsBehavior="Increment"
rollInterval="Day"
header=""
footer=""
formatter="Simple Text Formatter" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="{timestamp}{tab}{category}{tab}{message}"
name="Simple Text Formatter" />
</formatters>
<categorySources>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Full Trace Listener" />
<add name="Error Trace Listener" />
</listeners>
</allEvents>
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Error Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
Switching configuration - full to just errors
There are different ways to do this, so this is just the way I found works for me.
There are two listeners, for the error file and the full file. The error listener has a filter="Error" on it so it only writes errors. specialSources / allEvents just sends everything (switchValue="All") through to both listeners. To switch to error-only logging, just delete the full trace listener and (optionally as we already do this in the listener) change to allEvents switchValue="Error".
<allEvents switchValue="Error" name="All Events">
<listeners>
<!--<add name="Full Trace Listener" />-->
<add name="Error Trace Listener" />
</listeners>
</allEvents>