36

I was just wondering why there isn't a trace level in log4Net. This level seems to be missing and I sometimes feel the need to use it, for example to output what events are being executed in an application. This feature is a part of log4J.

I know I can create a custom level like is talked about here but I don't want to put time and effort in something I feel should be part of the library itself.

Do you know about a log4net extension library that implements this or why this wasn't a part of the port to .net ?

Jaymin
  • 2,879
  • 3
  • 19
  • 35
armannvg
  • 1,736
  • 1
  • 15
  • 29

3 Answers3

66

You can add a Verbose (or Trace level) to log4net by using extension methods. This is what I'm using:

public static class ILogExtentions
{       
    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);
    }

}

Usage example:

public class ClientDAO
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ClientDAO));

    public void GetClientByCode()
    {
        log.Trace("your verbose message here");
        //....
    }
}

Source:

http://www.matthewlowrance.com/post/2010/07/14/Logging-to-Trace-Verbose-etc-with-log4net.aspx

Bert
  • 861
  • 9
  • 22
  • Is it possible to get an IsTraceEnabled flag to query if TRACE level is enabled in the logging? – galmok Oct 06 '15 at 10:35
  • Never mind, extension properties do not exist yet. It has to be an extension method it seems. – galmok Oct 06 '15 at 10:50
  • 1
    it seems that the private field log is never used. – Can Bud Feb 27 '17 at 10:59
  • why`System.Reflection.MethodBase.GetCurrentMethod().DeclaringType`? That is an inefficient way of just writing `typeof(ILogExtentions)` – poizan42 Jul 22 '19 at 12:23
  • 1
    @poizan42 that is not correct. The reflection accesses the type which declared the logger instance. For example the `MyLoggingClass` in `ILog Log = LogManager.GetLogger(typeof(MyLoggingClass));` – Trinova Feb 04 '20 at 15:47
  • @Trinova no it doesn't. GetCurrentMethod returns a MethodInfo representing the ILogExtentions.Trace/Verbose method. The declaring type of that is obviously ILogExtentions. – poizan42 Feb 04 '20 at 17:15
  • - and actually you may end up getting the caller instead if the JIT decides to inline the method, see https://stackoverflow.com/a/41460315/1555496 – poizan42 Feb 04 '20 at 17:18
  • @galmok yes there is a way to get this: Log.Logger.IsEnabledFor(log4net.Core.Level.Trace); – Justin May 12 '21 at 17:41
14

There is a trace level in the log4net.Core.Level class http://logging.apache.org/log4net/release/sdk/html/F_log4net_Core_Level_Trace.htm

nos
  • 223,662
  • 58
  • 417
  • 506
9

The log4net.ILog interface only exposes methods and properties for Fatal, Error, Warn, Info and Debug levels.

I agree this is a bit limiting and would like to see one more level in there. But then the optimal number of levels is probably "one more than the current number of levels" - you'll always find yourself occasionally wanting one more level.

Jaymin
  • 2,879
  • 3
  • 19
  • 35
Joe
  • 122,218
  • 32
  • 205
  • 338
  • 1
    Yeah I agree, you are probably left with wanting more. Strange though that they left TRACE out of the ILog interface since it's available in the SDK as nos pointed out in his answer – armannvg Sep 09 '09 at 10:41
  • 3
    I suspect it was added to the SDK after the ILog interface was designed. – Joe Sep 09 '09 at 16:54