4.使用应用程序块
当配置完成后,看看App.config配置文件
编写一个简单的日志应用程序:<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourc eSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" /> </configSections> <enterpriseLibrary.ConfigurationSource selectedSource="Sql Configuration Source"> <sources> <add name="Sql Configuration Source" t="" ype="Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource.SqlConfigurationS ource, Microsoft.Practices.EnterpriseLibrary.SqlConfigurationSource, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" connectionString="Password=19811218;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=RJ-097" getStoredProcedure="EntLib_GetConfig" setStoredProcedure="EntLib_SetConfig" refreshStoredProcedure="UpdateSectionDate" removeStoredProcedure="EntLib_RemoveSection" /> <add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfiguratio nSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" /> </sources> </enterpriseLibrary.ConfigurationSource> </configuration>
运行程序后,在trace.log中记录了这样的一条日志信息class Program ...{ static void Main(string[] args) ...{ LogEntry log = new LogEntry(); log.Message = "This is a test!"; Logger.Write(log); } }
----------------------------------------
General Information: 0 : Timestamp: 2006-7-4 8:02:05
Message: This is a test!
Category: General
Priority: -1
EventId: 0
Severity: Information
Title:
Machine: RJ-097
App Domain: EntLibDemo1.vshost.exe
ProcessId: 5896
Process Name: D:\Visual Studio2005 Project\EntLibDemo1\EntLibDemo1\bin\Debug\EntLibDemo1.vshost.exe
Thread Name:
Win32 ThreadId:4304
Extended Properties:
----------------------------------------
General Information: 0 : Timestamp: 2006-7-4 8:02:05
Message: This is a test!
Category: General
Priority: -1
EventId: 0
Severity: Information
Title:
Machine: RJ-097
App Domain: EntLibDemo1.vshost.exe
ProcessId: 5896
Process Name: D:\Visual Studio2005 Project\EntLibDemo1\EntLibDemo1\bin\Debug\EntLibDemo1.vshost.exe
Thread Name:
Win32 ThreadId:4304
Extended Properties:
----------------------------------------
我们就可以把配置信息保存到了SQL Server数据库中,也许有人觉得这个技巧不存在什么实际意义,但是特殊情况下,当你需要这样实现的时候,别忘了这篇Post。