首页 文章

为什么log4Net中没有跟踪级别?

提问于
浏览
29

我只是想知道为什么log4Net中没有trace level . 这个级别似乎缺失了,我有时觉得需要使用它,例如输出应用程序中正在执行的事件 . 此功能是part of log4J .

我知道我可以创建一个自定义级别,就像被谈论here但我不想把时间和精力放在我认为应该成为库本身的一部分的东西上 .

你知道一个实现这个的log4net扩展库,或者为什么这不是.net端口的一部分?

3 回答

  • 13

    您可以使用扩展方法向log4net添加详细(或跟踪级别) . 这就是我正在使用的:

    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);
        }
    
    }
    

    用法示例:

    public class ClientDAO
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ClientDAO));
    
        public void GetClientByCode()
        {
            log.Trace("your verbose message here");
            //....
        }
    }
    

    资源:

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

  • 9
  • 48

    log4net.ILog接口仅公开Fatal,Error,Warn,Info和Debug级别的方法和属性 .

    我同意这有点限制,并希望在那里再看一个级别 . 但是,最佳等级数可能是“比目前的等级数多一个” - 你总会发现自己偶尔会想要一个等级 .

相关问题