2009-09-08 77 views
27

我只是想知道爲什麼log4Net中沒有trace level。這個級別似乎缺失了,有時我覺得需要使用它,例如輸出在應用程序中正在執行的事件。此功能是part of log4J爲什麼log4Net中沒有跟蹤級別?

我知道我可以創建一個自定義關卡,就像談到here一樣,但是我不想把時間和精力放在我認爲應該成爲圖書館本身的一部分的東西上。

你知道一個log4net擴展庫,它實現了這個或爲什麼這不是.net端口的一部分嗎?

回答

9

的log4net.ILog接口只公開的方法和屬性致命,錯誤,警告,信息和調試水平的跟蹤級別。

我同意這是有點限制,並希望在那裏看到更多的水平。但是,最佳數量的水平可能是「比當前水平數量多一個」 - 你總是會發現自己偶爾想要更多的水平。

+1

是的,我同意,你可能還想要更多。奇怪的是,他們讓TRACE退出了ILog界面,因爲它在SDK中可用,因爲他的回答中提到了 – armannvg 2009-09-09 10:41:36

+3

,我懷疑它是在設計ILog界面之後添加到SDK中的。 – Joe 2009-09-09 16:54:56

46

您可以使用擴展方法將詳細信息(或跟蹤級別)添加到log4net。這是我使用的是什麼:

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

} 

用例:

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

+0

如果在日誌記錄中啓用TRACE級別,是否可以獲取IsTraceEnabled標誌來查詢? – galmok 2015-10-06 10:35:58

+0

沒關係,擴展屬性還不存在。它似乎是一個擴展方法。 – galmok 2015-10-06 10:50:10

+1

似乎從未使用私人字段日誌。 – 2017-02-27 10:59:02

相關問題