The following is an example of extending the ILog interface in the log4net libraries.
See
this blog entry for an explination.
public static class LogExtension
{
/// <summary>
/// Logs an exception and then returns that exception
/// </summary>
/// <span name="logger" class="mceItemParam"></span>The logger this method overrides.</param>
/// <span name="message" class="mceItemParam"></span>The message to log.</param>
/// <span name="exception" class="mceItemParam"></span>The exception to log.</param>
/// <returns>The <see cref="exception"/> passed in, unmodified</returns>
public static Exception Exception(this ILog logger, Exception exception, string message )
{
logger.Error(message, exception);
return exception;
}
/// <summary>
/// Logs an exception and then returns that exception
/// </summary>
/// <span name="logger" class="mceItemParam"></span>The logger this method overrides.</param>
/// <span name="exception" class="mceItemParam"></span>The exception to log.</param>
/// <span name="fmt" class="mceItemParam"></span>The format string for the message to log.</param>
/// <span name="arg0" class="mceItemParam"></span>The argument for the format string.</param>
/// <returns>
/// The <see cref="exception"/> passed in, unmodified
/// </returns>
public static Exception Exception(this ILog logger, Exception exception, string fmt, object arg0)
{
logger.Error(string.Format(fmt, arg0), exception);
return exception;
}
/// <summary>
/// Logs an exception and then returns that exception
/// </summary>
/// <span name="logger" class="mceItemParam"></span>The logger this method overrides.</param>
/// <span name="exception" class="mceItemParam"></span>The exception to log.</param>
/// <span name="fmt" class="mceItemParam"></span>The format string for the message to log.</param>
/// <span name="arg0" class="mceItemParam"></span>The first argument for the format string.</param>
/// <span name="arg1" class="mceItemParam"></span>The second argument for the format string.</param>
/// <returns>
/// The <see cref="exception"/> passed in, unmodified
/// </returns>
public static Exception Exception(this ILog logger, Exception exception, string fmt, object arg0, object arg1)
{
logger.Error(string.Format(fmt, arg0,arg1), exception);
return exception;
}
/// <summary>
/// Logs an exception and then returns that exception
/// </summary>
/// <span name="logger" class="mceItemParam"></span>The logger this method overrides.</param>
/// <span name="exception" class="mceItemParam"></span>The exception to log.</param>
/// <span name="fmt" class="mceItemParam"></span>The format string for the message to log.</param>
/// <span name="arg0" class="mceItemParam"></span>The first argument for the format string.</param>
/// <span name="arg1" class="mceItemParam"></span>The second argument for the format string.</param>
/// <span name="arg2" class="mceItemParam"></span>The third argument for the format string.</param>
/// <returns>
/// The <see cref="exception"/> passed in, unmodified
/// </returns>
public static Exception Exception(this ILog logger, Exception exception, string fmt, object arg0, object arg1, object arg2)
{
logger.Error(string.Format(fmt, arg0,arg1, arg2), exception);
return exception;
}
/// <summary>
/// Logs an exception and then returns that exception
/// </summary>
/// <span name="logger" class="mceItemParam"></span>The logger this method overrides.</param>
/// <span name="exception" class="mceItemParam"></span>The exception to log.</param>
/// <span name="fmt" class="mceItemParam"></span>The format string for the message to log.</param>
/// <span name="args" class="mceItemParam"></span>The arguments for the format string.</param>
/// <returns>
/// The <see cref="exception"/> passed in, unmodified
/// </returns>
public static Exception Exception(this ILog logger, Exception exception, string fmt, params object[] args)
{
logger.Error(string.Format(fmt, args), exception);
return exception;
}
}
log4net-Extension-Code.aspx