2009-10-05 39 views
1

我寫了一個簡單的log4net包裝。我想知道這個包裝代碼是否可以改進。這個簡單的Log4Net包裝可以改進嗎?

我有點擔心引入到每個日誌記錄功能(信息,警告等)的反射代碼來獲取調用函數名稱。由於這個原因,是否可能有任何可能的性能問題?

namespace Acqueon.Pacer.Core.Helpers 
{ 
    #region Imports 

    using System; 
    using System.Diagnostics; 
    using System.Reflection; 

    using log4net; 

    #endregion 

    /// <summary> 
    /// log4net Log helper 
    /// </summary> 
    public sealed class Logger 
    { 
     #region Constants and Fields 

     /// <summary> 
     /// Determines whether the DEBUG Mode is enabled. 
     /// </summary> 
     private readonly bool isDebugEnabled; 

     /// <summary> 
     /// The is error enabled. 
     /// </summary> 
     private readonly bool isErrorEnabled; 

     /// <summary> 
     /// Determines whether the FATAL Mode is enabled. 
     /// </summary> 
     private readonly bool isFatalEnabled; 

     /// <summary> 
     /// Determines whether the INFO Mode is enabled. 
     /// </summary> 
     private readonly bool isInfoEnabled; 

     /// <summary> 
     /// Determines whether the WARN Mode is enabled. 
     /// </summary> 
     private readonly bool isWarnEnabled; 

     /// <summary> 
     /// The logger object 
     /// </summary> 
     private readonly ILog log; 

     #endregion 

     #region Constructors and Destructors 

     /// <summary> 
     /// Initializes a new instance of the Logger class. 
     /// </summary> 
     public Logger() 
      : this(new StackTrace().GetFrame(1).GetMethod().DeclaringType) 
     { 
     } 

     /// <summary> 
     /// Initializes a new instance of the Logger class. 
     /// </summary> 
     /// <param name="type"> 
     /// The type of logger. 
     /// </param> 
     public Logger(Type type) 
     { 
      this.log = LogManager.GetLogger(type); 

      this.isDebugEnabled = this.log.IsDebugEnabled; 
      this.isErrorEnabled = this.log.IsErrorEnabled; 
      this.isInfoEnabled = this.log.IsInfoEnabled; 
      this.isFatalEnabled = this.log.IsFatalEnabled; 
      this.isWarnEnabled = this.log.IsWarnEnabled; 
     } 

     #endregion 

     #region Public Methods 

     /// <summary> 
     /// Logs the debug message. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     public void Debug(string message) 
     { 
      if (this.isDebugEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Debug(methodBase.Name + " : " + message); 
      } 
     } 

     /// <summary> 
     /// Logs the debug message and the exception. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     /// <param name="exception"> 
     /// The exception. 
     /// </param> 
     public void Debug(string message, Exception exception) 
     { 
      if (this.isDebugEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Debug(methodBase.Name + " : " + message, exception); 
      } 
     } 

     /// <summary> 
     /// Logs the error message. 
     /// </summary> 
     /// <param name="errorMessage"> 
     /// The error message. 
     /// </param> 
     public void Error(string errorMessage) 
     { 
      if (this.isErrorEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Error(methodBase.Name + " : " + errorMessage); 
      } 
     } 

     /// <summary> 
     /// Logs the error message and the exception. 
     /// </summary> 
     /// <param name="errorMessage"> 
     /// The error message. 
     /// </param> 
     /// <param name="exception"> 
     /// The exception. 
     /// </param> 
     public void Error(string errorMessage, Exception exception) 
     { 
      if (this.isErrorEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Error(methodBase.Name + " : " + errorMessage, exception); 
      } 
     } 

     /// <summary> 
     /// Logs the fatal error message. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     public void Fatal(string message) 
     { 
      if (this.isFatalEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Fatal(methodBase.Name + " : " + message); 
      } 
     } 

     /// <summary> 
     /// Logs the fatal error message and the exception. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     /// <param name="exception"> 
     /// The exception. 
     /// </param> 
     public void Fatal(string message, Exception exception) 
     { 
      if (this.isFatalEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Fatal(methodBase.Name + " : " + message, exception); 
      } 
     } 

     /// <summary> 
     /// Logs the info message. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     public void Info(string message) 
     { 
      if (this.isInfoEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Info(methodBase.Name + " : " + message); 
      } 
     } 

     /// <summary> 
     /// Logs the info message and the exception. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     /// <param name="exception"> 
     /// The exception. 
     /// </param> 
     public void Info(string message, Exception exception) 
     { 
      if (this.isInfoEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Info(methodBase.Name + " : " + message, exception); 
      } 
     } 

     /// <summary> 
     /// Logs the warning message. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     public void Warn(string message) 
     { 
      if (this.isWarnEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Warn(methodBase.Name + " : " + message); 
      } 
     } 

     /// <summary> 
     /// Logs the warning message and the exception. 
     /// </summary> 
     /// <param name="message"> 
     /// The message. 
     /// </param> 
     /// <param name="exception"> 
     /// The exception. 
     /// </param> 
     public void Warn(string message, Exception exception) 
     { 
      if (this.isWarnEnabled) 
      { 
       MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
       this.log.Warn(methodBase.Name + " : " + message, exception); 
      } 
     } 

     #endregion 
    } 
} 
+3

目的是什麼? AFAIK log4net可以爲你做這些事情的大部分。 – Grzenio

+0

目的是..我想每個類有一個記錄器對象,不想把檢查的模式.. – Zuhaib

回答

5

爲什麼你就不能使用這個:

以下的PatternLayout圖案提取位置信息:

%F用於輸出,其中發出日誌請求的文件名

%L用於輸出日誌請求爲 的行號

%M用於輸出方法發出日誌記錄請求的名稱

%C用於輸出發出 日誌記錄請求的呼叫者的標準類名稱。

請注意,在這兩種情況下,需要堆棧走路,這是昂貴的。

<appender name="DebugOut" 
     type="log4net.Appender.OutputDebugStringAppender"> 
<layout type="log4net.Layout.PatternLayout"> 
    <conversionPattern value="%-5p [%t] %C{1}.%M - %m%n" /> 
</layout> 

+0

%M將給輔助函數的名稱..如果我使用包裝log4net – Zuhaib

+0

謝謝..我會從幫手中刪除反射部分 – Zuhaib

+1

@ZuhaibZ - 我認爲重點是整個班級都是不必要的。正如Grzenio所說,Log4Net將爲你做到這一切。 – TrueWill

1

最終將會從反射代碼的性能損失,顯然這取決於你有多少日誌消息在整個應用程序日誌。

您也可以使用正確的轉換模式輸出行號,基本調用堆棧和方法信息。該功能默認情況下存在於log4net中 - 文檔警告您存在性能問題。

最後,我將在Exception上實現包裝類作爲擴展方法。

+0

感謝擴展方法提示.. dono爲什麼我沒有想到:) – Zuhaib

+0

「最後,我會實現你的包裝類作爲擴展方法在異常」 - 爲什麼? Log4Net不僅用於記錄異常。 – Joe

+0

@Joe - 當然不是,顯然你會在有趣的地方記錄你想要的東西,在你的代碼中有很多debug/info語句。 我總是發現,任何時候我想要輸出堆棧跟蹤信息或類似的東西,這是當意想不到的事情發生時,它總是處於某種異常。此外,將所有這些封裝在一個類中,可以讓您準確更改您在整個代碼庫中收集的堆棧跟蹤信息。在這裏閱讀更多:http://elegantcode.com/2009/07/31/exception-logging-with-entlib-made-simple/ –

相關問題