2013-09-30 29 views
0

要整合NLog,我們要定義一個接口,並且正在考慮兩種方法。 我們使用C#。NLog的集成:使用接口的方法或使用枚舉?

方法1:

public interface ILoggingManager 
{ 
    void DoErrorLogging(type , string) 
    void DoErrorLogging(type , string , exception) 

    void DoTraceLogging(type , string) 
    void DoTrace Logging(type , string , exception) 

    // And so on for all the types that Nlog supports. 
    .... 
    // Finally would have 10 methods defined in this interface. 
} 

方法2:

//Have an Enum defined for the logging levels 
public enum LoggingLevel 
{ 
    Error, 
    Warn, 
    Info, 
    Debug, 
    Trace 
} 

public interface ILoggingManager 
{ 
    void DoLogging(type , LoggingLevel , string) 
    void DoLogging(type , LoggingLevel , exception) 
} 

問題:

  1. 這是在頭腦更好的方法保持設計原則(如固體)?
  2. 哪種方法在性能方面更好?

回答

0

我的回答並沒有提供直接的解決方案,但仍...

我會用一個現有日誌框架,包括所有的接口。其餘的是(或者應該是)配置。

我建議你要麼使用log4netslf

如果你堅持你自己的界面,那麼我認爲性能是不是一個問題,在這裏都沒有。您可以將枚舉值映射到NLog的合適方法(方法2),也可以直接使用它(方法1)。

方法2是一個更清潔,毫無疑問。我想有點改變它:

public interface ILoggingManager 
{ 
    void DoLogging(type , LoggingLevel , string, /* optional */ exception) 
} 

我會介紹我在方法2,見好處有時候,你不知道前面是什麼你將要使用的日誌記錄級別。假設您正在檢查異常。有時你最終會遇到Warn,有時候最終會以Error結束。如果您使用方法2,則代碼重用更簡單:只需選擇相關的枚舉值並設置即可。如果您使用方法1,代碼重用可能包括一個映射到Action相關的方法,並調用它,這讓整個事情有點太複雜了記錄......

順便說一句,什麼是type這裏?消費類的類型?

+0

Ron,我們打算使用Nlog框架,日誌級別被映射到NLog的日誌級別,只是我們正在編寫一個接口,我們想知道哪種方法更好。爲什麼第二種方法是清潔劑? – siva

+0

是的,我在這裏指的類型是類型的conuming類 – siva

+0

@siva,你可以通過反射檢查消費類,但那是另一回事...... –