简体   繁体   中英

Why isn't there a trace level in log4Net?

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 ?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM