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 would like to use your ILogExtensions example. I just need a little more help to get started.

My current situation:

  1. I have an already pre-build log4net library which I cannot change
  2. I have an application that uses the log4net library from the point above
  3. I would like to extend log4net to have a Trace and Verbose logging level

I have the following questions:

  1. What exactly do I need to do with your ILogExtensions class?
  2. Can I create another library from that to use it in my application? This would result in three libraries, log4net, ILogExtensions and my application.
  3. What namespace to I need to use for ILogExtensions?

I have something like this in my head, but cannot get around the questions above:

using log4net;

namespace log4net.Ext
{
    public static class ILogExtentions
    {
        ...
    }
}

I would really appreciate your help!

Many thanks
Konstantin

@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