Enterprise Library日志块的编程配置

Dav*_*d W 3 c# logging unit-testing enterprise-library

我以前使用过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());
  }
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*nro 8

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