在以前的Enterprise Library版本中,使用各个BLOCK需要配置XML.否则很难正常工作.在Enterprise Library 5.0中提供了一个ConfigurationSourceBuilder的class来扮演configuration sources.这样就可以实现Fluent配置API驱动Enterprise Library 5.0.
也就是我们可以在程序中动态配置或替换这个参数.下面以Logging Application Block为例,看这个Unit Test:

   1:          [Test]
   2:          public void TestLoggingWithFluentConfigurationAPI()
   3:          {
   4:              var builder = new ConfigurationSourceBuilder();
   5:              builder.ConfigureLogging()
   6:                  .WithOptions
   7:                  .DoNotRevertImpersonation()
   8:                  .LogToCategoryNamed("Basic")
   9:                  .SendTo.FlatFile("Basic Log File")
  10:                  .FormatWith(new FormatterBuilder()
  11:                                  .TextFormatterNamed("Text Formatter")
  12:                                  .UsingTemplate(
  13:                                      "Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}"))
  14:                  .ToFile("d:\\logs\\BasicTest.log")
  15:                  .SendTo.RollingFile("Rolling Log files")
  16:                  .RollAfterSize(1024)
  17:                  .ToFile("d:\\logs\\RollingTest.log")
  18:                   .LogToCategoryNamed("General")
  19:           .WithOptions.SetAsDefaultCategory()
  20:           .SendTo.SharedListenerNamed("Basic Log File");
  21:   
  22:              var configSource = new DictionaryConfigurationSource();
  23:              builder.UpdateConfigurationWithReplace(configSource);
  24:   
  25:              EnterpriseLibraryContainer.Current
  26:                = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
  27:              LogWriterFactory logFactory = new LogWriterFactory(configSource);
  28:              LogWriter logWriter = logFactory.CreateDefault();
  29:              logWriter.Write("This is test message", "Basic");
  30:              logWriter.Write("This is default message");
  31:   
  32:              string logfilepath = Path.Combine("d:", "logs\\BasicTest.log");
  33:              Assert.IsTrue(File.Exists(logfilepath));
  34:              Assert.IsTrue(File.Exists("d:\\logs\\RollingTest.log"));
  35:   
  36:          }

首先你必须引用

Microsoft.Practices.ServiceLocation
Microsoft.Practices.EnterpriseLibrary.Logging
Microsoft.Practices.EnterpriseLibrary.Common

我们看上面的CODE,从第5行到20行,我们配置一个ConfigurationSourceBuilder,并且全是使用Fluent配置API. 我们配置日志的分类,Listener,格式,保到文件名.
我们指定了保存到D盘的logs的文件夹中.运行后我们看到BasicTest.log文件中内容如下:

----------------------------------------
Timestamp: 2010-12-14 3:23:29
Message: This is test message
Category: Basic

----------------------------------------
----------------------------------------
Timestamp: 2010-12-14 3:23:29
Message: This is default message
Category: General

----------------------------------------

因为我们只保留了三个关键属性,第一条日志第29行代码所写,我们指定了一个"Basic”的分类,第二条日志是第30行代码所写,它写到默认的分类(General)中了.再看
RollingTest.log文件内容如下:

----------------------------------------
Basic Information: 1 : Timestamp: 2010-12-14 3:23:29
Message: This is test message
Category: Basic
Priority: -1
EventId: 1
Severity: Information
Title:
Machine: USER
App Domain: domain-nunit.tdnet.dll
ProcessId: 3672
Process Name: D:\Program Files\TestDriven.NET 3\ProcessInvocation86.exe
Thread Name: TestRunnerThread
Win32 ThreadId:2268
Extended Properties:
----------------------------------------

因为第15行代码,我们同时为Basic这个分类设置了RollingFile,所以你可以看到这个使用默认格式的内容,注意第20行代码,SharedListenerNamed("Basic Log File");
这个代表我们共享的同一个Basic Log File Listener . 代码第23行,我们替换更新这些配置,最后创建LogFactory实现写日志.

还有更多的例子,请看:

  • The Caching Application Block
  • The Cryptography Application Block
  • The Data Access Application Block
  • The Exception Handling Block
  • The Logging Application Block
  • The Security Application Block
  • 您可以感兴趣的文章:

    无配置文件使用Enterprise Library Logging Application Block 4.1

    希望对您开发有帮肋.


    作者:Petter Liu
    出处:http://www.cnblogs.com/wintersun/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    该文章也同时发布在我的独立博客中-Petter Liu Blog

    posted on 2010-12-14 11:40  PetterLiu  阅读(933)  评论(0编辑  收藏  举报