使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件
这里只是说明在项目中如何配置使用微软企业库的日志组件,对数据库方面的配置请参考其他资料。
1、在项目中添加Microsoft.Practices.EnterpriseLibrary.Data.dll、Microsoft.Practices.EnterpriseLibrary.Logging.dll、Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll这三个引用。

2、打开EnterpriseLibrary的配置工具EntLibConfig.exe
1)选择菜单“Block->Add Logging Setting"

2)点击“+”号添加Logging Target Listeners,选择Add Database Trace Listener

3、设置Database Trace Listener中的参数,比如数据库连接、插入日志存储过程、插入分类存储过程、选择文本格式等
4、设置Database Setting中的“Connection String”中的数据库连接
5、最后保存配置文件到项目路径中。
设置后的配置文件:
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="" machineName="." traceOutputOptions="None" />
<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
databaseInstanceName="Connection String" writeLogStoredProcName="EL_WRITELOG"
addCategoryStoredProcName="EL_ADDCATEGORY" formatter="Text Formatter" />
<add name="XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="test.xml" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="<TIMESTAMP> {timestamp}</TIMESTAMP> {newline} <MESSAGE> {message}</MESSAGE>{newline} <CATEGORY>{category}</CATEGORY>{newline} <PRIORITY>{priority}</PRIORITY>{newline} <EVENTID>{eventid}</EVENTID>{newline} <SEVERITY>{severity}</SEVERITY>{newline} <TITLE>{title}</TITLE>{newline} <MACHINE>{localMachine}</MACHINE>{newline} <APP DOMAIN> {localAppDomain}</APP DOMAIN>{newline} <PROCESSID>{localProcessId}</PROCESSID>{newline} <PROCESS NAME> {localProcessName}</PROCESS NAME> {newline} <THREAD NAME> {threadName}</THREAD NAME>{newline} <WIN32 THREADID>{win32ThreadId}</WIN32 THREADID>{newline} <EXTENDED PROPERTIES> {dictionary(<KEY>{key}</KEY> - <VALUE>{value}</VALUE>{newline})}</EXTENDED PROPERTIES>"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Database Trace Listener" />
<add name="XML Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Database Trace Listener" />
<add name="XML Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<dataConfiguration defaultDatabase="Connection String" />
<connectionStrings>
<add name="Connection String" connectionString="DATA SOURCE=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = eifoclog)));PERSIST SECURITY INFO=True;USER ID=FOC;Password=foc"
providerName="System.Data.OracleClient" />
</connectionStrings>
</configuration>
需要注意的是:
在工具中只能选一个监听器,但实际可以有多个监听器同时监听。
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Database Trace Listener" />
<add name="XML Trace Listener" />
</listeners>
</add>
</categorySources>
在项目中使用:
class Program
{
static void Main(string[] args)
{
LogEntry logEntry = new LogEntry();
logEntry.EventId = ;
logEntry.Priority = ;
logEntry.Severity = System.Diagnostics.TraceEventType.Error;
logEntry.Title = "标题";
logEntry.Message = "test";
logEntry.Categories.Add("C#学习");
logEntry.Categories.Add("Microsoft Enterprise Library学习"); Logger.Writer.Write(logEntry, "General");
Console.WriteLine("日志写入完成!");
}
}
本文参考博客地址:http://www.cnblogs.com/huangcong/archive/2010/06/04/1751087.html
示例代码地址:http://files.cnblogs.com/qiu2013/ConsoleApplication1.zip
使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件的更多相关文章
- 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持
在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...
- 基于微软企业库的AOP组件(含源码)
软件开发,离不开对日志的操作.日志可以帮助我们查找和检测问题,比较传统的日志是在方法执行前或后,手动调用日志代码保存.但自从AOP出现后,我们就可以避免这种繁琐但又必须要实现的方式.本文是在微软企业库 ...
- 权限管理系统源码分析(ASP.NET MVC 4.0 + easyui + EF6.0 + MYSQL/MSSQLSERVER +微软企业库5.0+日志绶存)
系统采用最先进技术开发: (ASP.NET MVC 4.0 + easyui + EF6.0 + MYSQL/MSSQLSERVER +微软企业库5.0+日志绶存) 大家可以加我QQ讨论 309159 ...
- 微软企业库Microsoft Enterprise Library的相关文章链接
微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...
- [EntLib]微软企业库5.0 学习之路——第一步、基本入门
话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...
- 微软企业库5.0学习-Security.Cryptography模块
一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...
- 微软企业库的Cache
微软企业库的Cache 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSamp ...
- Prism6下的MEF:基于微软企业库的Cache
通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSample.Infrastru ...
- .NET 类库研究必备参考 添加微软企业库源码
前不久,为大家提供了一个.NET 类库参考源码的网站,扣丁格鲁(谐音“coding guru”),使用了段时间,发现一些不方便的地方,特意做了一些更改,希望大家多提意见,下面是此次更改的地方. 更改1 ...
随机推荐
- 每天一个Linux命令(6):rmdir命令
rmdir命令用来删除空目录 注意:子目录被删除之前应该是空目录.就是说,该目录中的所有文件必须用rm命令全部,另外,当前工作目录必须在被删除目录之上,不能是被删除目录本身,也不能是被删除目录的子目录 ...
- CALayer层的属性(转)
一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint position: (1)用来设置CALayer在父层中的 ...
- 嵌入式:指针的指针、链表、UCOS 的 OSMemCreate 。
初看,UCOS 的 OSMemCreate 代码,感觉有点怪怪的,比如,把 指针指向的地址 强制转换成 指针的指针的指向地址 ?那转换后 指针的指针 又是什么? void OSMemCreate (O ...
- PP: 混合生产方式(MTO与MTS为例)(转)
http://blog.sina.com.cn/s/blog_4c01b7650100yf1d.html PP: 混合生产方式(MTO与MTS为例) 一.业务概览某公司生产的同一种产品正常情况下客户无 ...
- 台式机上如何配置并使用苹果iPhone的耳机麦克风 并且麦克风开启降噪功能
这个资料和技巧在网络上面很少有人分享,但是可能会有不少人需要这个东西.这里分享下经验.这也是一个困扰我很久的一个问题.因为买来了这个转接头,发现,录音的时候iPhone的耳机麦克风有很大的噪音无法消除 ...
- 使用jQuery实现数字逆时针旋转
要实现数字逆转,最主要是分析我们页面的元素结果,结合选择器充分利用起来! 例如:以下lable中每一个id和值的安排具有一定结构的意义需要用心分析: jQuery代码:
- django的HttpResponse对象
服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpRequest对象,在django. ...
- 模块的使用与orm简介
目录 1 django中app的概念: 2 模板路径配置: 3 静态文件配置: 4 完整版登录功能 5 get请求和post请求 6 新手三件套总结 7 pycharm连接mysql 8 orm介绍 ...
- latex02-LaTeX源文件的基本结构
1.一个latex文件有且仅有一个document环境 %后表示注释 2.latex的导言区用于全局设置. 例如:title\author\date 加入空行是结构更加清晰 为了能正确的使用标题信息, ...
- 初步学习pg_control文件之二
接前文:初步认识pg_control文件 继续学习,pg_control文件在何处形成的?是在initdb的时候,运用的函数如下: /* * This func must be called ONCE ...