2009-11-18 70 views
3

我以前使用過log4net,但是我的當前僱主使用企業庫應用程序塊。我以前開發的單元測試如下我的核心日誌類,想知道是否有人知道等值以下OneTimeSetup代碼記錄應用程序塊(抱歉長碼後):企業庫日誌塊的編程配置

public abstract class DataGathererBase 
{ 
    public readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
    public void CollectData() 
    { 
    this.LogDebug("Initialize started"); 
    } 

    public static class Logger 
    { 
    private static LoggingSettings settings = LoggingSettings.GetLoggingSettings(new SystemConfigurationSource()); 

    static Logger() 
    { 
     log4net.Config.XmlConfigurator.Configure(); 
    } 

    public static void LogDebug(this DataGathererBase current, string message) 
    { 
     if (current.logger.IsDebugEnabled) 
     { 
     current.logger.Debug(string.Format("{0} logged: {1}", current.GetType().Name, message)); 
     } 
    } 
    } 

[TestFixture] 
public class LoggerTests:DataGathererBase 
{ 
    private ListAppender appender; 
    private static ILog log; 

    [TestFixtureSetUp] 
    public void OneTimeSetup() 
    { 
    appender = new ListAppender(); 
    appender.Layout = new log4net.Layout.SimpleLayout(); 
    appender.Threshold = log4net.Core.Level.Fatal; 
    log4net.Config.BasicConfigurator.Configure(appender); 
    log = LogManager.GetLogger(typeof(ListAppender)); 
    } 

    [Test] 
    public void TestLogging() 
    { 
    this.LogDebug("Debug"); 
    Assert.AreEqual(0, ListAppender.logTable.Count()); 
    } 
} 

回答

1

給予信貸,這個答案是基於David Hayden article,它基於Alois Kraus article, Programatic Configuraton - Enterprise Library (v2.0) Logging Block。請閱讀這兩篇文章,詳細瞭解對Enterprise Library日誌的編程訪問。

我不熟悉ListAppender所以我創建的那支在列表中的日誌消息CustomTraceListener <字符串>:

public class ListAppender : Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener 
    { 
    private List<string> list = new List<string>(); 

    public override void Write(string message) 
    { 
    } 

    public override void WriteLine(string message) 
    { 
     list.Add(message); 
    } 

    public List<string> LogTable 
    { 
     get 
     { 
     return list; 
     } 
    } 
    } 


這裏是一個修改LoggerTests類編程訪問EL日誌類設置測試(這並不使用NUnit):

public class LoggerTests 
    { 
    private ListAppender appender; 
    private static LogWriter log; 

    public void OneTimeSetup() 
    { 
     appender = new ListAppender(); 

     // Log all source levels 
     LogSource mainLogSource = new LogSource("MainLogSource", SourceLevels.All); 
     mainLogSource.Listeners.Add(appender); 

     // All messages with a category of "Error" should be distributed 
     // to all TraceListeners in mainLogSource. 
     IDictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>(); 
     traceSources.Add("Error", mainLogSource); 

     LogSource nonExistentLogSource = null;  
     log = new LogWriter(new ILogFilter[0], traceSources, nonExistentLogSource, 
         nonExistentLogSource, mainLogSource, "Error", false, false); 
    } 

    public void TestLogging() 
    { 
     LogEntry le = new LogEntry() { Message = "Test", Severity = TraceEventType.Information }; 
     le.Categories.Add("Debug"); 
     log.Write(le); 

     // we are not setup to log debug messages 
     System.Diagnostics.Debug.Assert(appender.LogTable.Count == 0); 

     le.Categories.Add("Error"); 
     log.Write(le); 

     // we should have logged an error 
     System.Diagnostics.Debug.Assert(appender.LogTable.Count == 1); 
    } 
    } 
8

企業庫5.0引入一個fluent interface可以b e用於以編程方式配置應用程序塊。你可能會發現這是一個更舒適的選擇。

相關問題