在原先的Enterprise
Library
本子中,使用各样BLOCK须要配备XML.不然很难平常工作.在Enterprise
Library
5.0
中提供了1个ConfigurationSourceBuilder的class来饰演configuration
sources.那样就能够完结Fluent配置API.aspx)驱动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,从第六行到20行,大家安排3个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


因为我们只保留了七个基本点个性,第③条日志第①9行代码所写,大家钦点了3个”Basic”的归类,第③条日志是第三0行代码所写,它写到暗中同意的分类(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:

因为第壹5行代码,大家同时为Basic这么些分类设置了RollingFile,所以您能够看看那几个应用暗中同意格式的内容,注意第壹0行代码,SharedListenerNamed(“Basic Log File”);
本条代表大家共享的同1个Basic Log File Listener .
代码第二3行,我们轮换更新这么些布署,最终制造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

相关文章

网站地图xml地图