企业库测井块进行编程配置 [英] Programmatic configuration of Enterprise Library logging block

查看:188
本文介绍了企业库测井块进行编程配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前用log4net的,但我现在的雇主使用企业库应用程序块。我以前开发的单元测试如下我的核心日志类,并想知道,如果有人知道下面的OneTimeSetup代码记录应用程序块的当量(抱歉长码后):

 公共抽象类DataGathererBase 
{
公共只读log4net.ILog记录= log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()。 DeclaringType);
公共无效CollectData()
{
this.LogDebug(初始化启动);
}

公共静态类的Logger
{
私有静态LoggingSettings设置= LoggingSettings.GetLoggingSettings(新SystemConfigurationSource());

静态记录仪()
{
log4net.Config.XmlConfigurator.Configure();
}

公共静态无效LogDebug(这DataGathererBase当前,字符串消息)
{
如果(current.logger.IsDebugEnabled)
{
current.logger.Debug(的String.Format({0}记录:{1},current.GetType()名称,消息));
}
}
}

[的TestFixture]
公共类LoggerTests:DataGathererBase
{
私人ListAppender追加程序;
私有静态的ILog日志;

[TestFixtureSetUp]
公共无效OneTimeSetup()
{
的appender =新ListAppender();
appender.Layout =新log4net.Layout.SimpleLayout();
appender.Threshold = log4net.Core.Level.Fatal;
log4net.Config.BasicConfigurator.Configure(追加程序);
日志= LogManager.GetLogger(typeof运算(ListAppender));
}

[测试]
公共无效TestLogging()
{
this.LogDebug(调试);
Assert.AreEqual(0,ListAppender.logTable.Count());
}
}


解决方案

要给予信贷,这个答案是基于大卫·海登的文章< /一>这是基于一个阿洛伊斯克劳斯文章,程序化Configuraton - 企业库(V2.0)日志程序块。阅读好好看看编程访问企业库记录的两篇文章。



我不熟悉ListAppender所以我创建了那支在日志消息CustomTraceListener清单<串GT;

 公共类ListAppender:Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener 
$ { b $ b私人列表<串GT;名单=新名单,LT;串>();

公共覆盖无效写入(字符串消息)
{
}

公共覆盖无效的WriteLine(字符串消息)
{
list.Add(消息);
}

公开名单<串GT; LogTable
{
得到
{
返回列表;
}
}
}



结果
给的是编程访问EL日志记录类设置修改LoggerTests类测试(不使用NUnit的):

 公共类LoggerTests 
{
私人ListAppender追加程序;
私有静态日志写日志;

公共无效OneTimeSetup()
{
的appender =新ListAppender();

//记录所有源层次
LOGSOURCE mainLogSource =新LOGSOURCE(MainLogSource,SourceLevels.All);
mainLogSource.Listeners.Add(追加程序);

//以错误类别的所有消息应该是分布式的
//在mainLogSource所有TraceListeners。
IDictionary的<字符串,LOGSOURCE> traceSources =新词典<字符串,LOGSOURCE>();
traceSources.Add(错误,mainLogSource);

LOGSOURCE nonExistentLogSource = NULL;
日志=新日志写(新ILogFilter [0],traceSources,nonExistentLogSource,
nonExistentLogSource,mainLogSource,错误,假的,假的);
}

公共无效TestLogging()
{
LogEntry乐=新LogEntry(){消息=测试,严重性= TraceEventType.Information};
le.Categories.Add(调试);
log.Write(LE);

//我们没有设置为记录调试消息
System.Diagnostics.Debug.Assert(appender.LogTable.Count == 0);

le.Categories.Add(错误);
log.Write(LE);

//我们应该记录一个错误,
System.Diagnostics.Debug.Assert(appender.LogTable.Count == 1);
}
}


I've previously used log4net, but my current employer uses Enterprise Library application blocks. I had previously developed unit tests for my core logging classes as follows and was wondering if someone knew the equivalent for the OneTimeSetup code below for the logging app block (sorry for the long code post):

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

解决方案

To give credit, this answer is based on a David Hayden article which is based on an Alois Kraus article, Programatic Configuraton - Enterprise Library (v2.0) Logging Block . Read those two articles for a good look at programmatic access to Enterprise Library logging.

I wasn't familiar with ListAppender so I created a CustomTraceListener that sticks the log messages in a List<string>:

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


Here is a modified LoggerTests class that programmatically accesses the EL logging classes to setup the tests (this does not use 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);
    }
  }

这篇关于企业库测井块进行编程配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆