Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Created August 20, 2012 09:46
Show Gist options
  • Save lkaczanowski/3402710 to your computer and use it in GitHub Desktop.
Save lkaczanowski/3402710 to your computer and use it in GitHub Desktop.
ILog extension for TRACE and VERBOSE log4net levels
public static class ILogExtentions
{
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Trace(this ILog log, string message, Exception exception)
{
log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
log4net.Core.Level.Trace, message, exception);
}
public static void Trace(this ILog log, string message)
{
log.Trace(message, null);
}
public static void Verbose(this ILog log, string message, Exception exception)
{
log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
log4net.Core.Level.Verbose, message, exception);
}
public static void Verbose(this ILog log, string message)
{
log.Verbose(message, null);
}
}
@mission2010
Copy link

Hello Łukasz,

I already figured out how to use the ILogExtensions class. The answers to my questions are:

  1. Just create a library from this class
  2. Yes
  3. It is up to the user to choose a namespace for the extension class

How to use it in the application?

It is necessary to import the namespace, in my case log4net.Ext. The application code would look like this.

using log4net.Ext;

public class MyApplication
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public void SomeMethod()
    {
        log.Trace("Trace log");
        log.Verbose("Verbose log");
    }
}

Would you know what to set in the config file for level to have verbose messages and switch off trace messages?

I tried the following:

<level value="Verbose" />

But it did not work. Any idea how I can go fix that?

Many thanks,
Konstantin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment