【问题标题】:Programmatic configuration of Enterprise Library logging block企业库日志记录块的编程配置
【发布时间】:2010-12-18 01:33:47
【问题描述】:

我以前使用过 log4net,但我现在的雇主使用 Enterprise Library 应用程序块。我之前为我的核心日志记录类开发了单元测试,如下所示,想知道是否有人知道下面 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());
  }
}

【问题讨论】:

    标签: c# unit-testing logging enterprise-library


    【解决方案1】:

    为了表扬,此答案基于David Hayden article,而Alois Kraus article, Programatic Configuraton - Enterprise Library (v2.0) Logging Block 又基于Alois Kraus article, Programatic Configuraton - Enterprise Library (v2.0) Logging Block 。阅读这两篇文章,了解对企业库日志的编程访问。

    我不熟悉 ListAppender,所以我创建了一个 CustomTraceListener,它将日志消息粘贴在 List 中:

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

    【讨论】:

      【解决方案2】:

      Enterprise Library 5.0 引入了fluent interface,可用于以编程方式配置应用程序块。您可能会发现这是一个更舒适的选择。

      【讨论】:

        猜你喜欢
        • 2015-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-19
        • 2011-03-29
        • 1970-01-01
        相关资源
        最近更新 更多