使用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 ...
随机推荐
- Sass 基础(一)
css 是一些非常简单得语句的组合,既然简单的语句,就不可避免的有很多重复的,冗余的东西,而且没有传统编程语言变量,控制语句等高级特性,所以造成了css 编写低效,往往需要查找替换,大量复制来修改或者 ...
- jquery mobile 移动web(6)
jquery mobile 针对移动端设备的事件类型. 1.touch 事件. tap 快速触摸屏幕并且离开,类似一种完整的点击操作. taphold 触摸屏幕并保持一段时间. swipe 在1秒内水 ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--E-回旋星空
链接:https://www.nowcoder.com/acm/contest/90/E 来源:牛客网 1.题目描述 曾经有两个来自吉尔尼斯的人(A和C)恋爱了,他们晚上经常在一起看头上的那片名为假的 ...
- 【原创】如何治疗使用python中re模块group、groups与findall分组匹配后产生的“眩晕反应”
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9244783.html 直接先上例子 >>> re.search('(book+ ...
- 访问本地方站出现EOF的分析和解决
每天早晨打开电脑运行本地项目的时候,有时候浏览器上会出现EOF 之前都都能正常访问,所以我猜想本地的项目本身肯定是没有问题的. Google了下,发现有人说是代理的问题,于是关闭代理试过后,发现可以访 ...
- jquery图片滚动normalizy.css
article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block; ...
- click与on的区别
click只能用于html在注册事件之后没有变化的:on用于html在注册事件后,还会通过JS脚本添加一些按钮,并者希望这些按钮也会有之前注册事件的按钮同样的事件话,就需要用on去为按钮的父节点去注册 ...
- C# Regex正则验证规则
using System; using System.Text.RegularExpressions; namespace MetarCommonSupport { /// <summary&g ...
- 查询表名里含有Bill的表有哪些
Select Name from Master.dbo.sysobjects where xtype='u' and Name like '%Bill%' order by name
- 洛谷 T51922 父子
题目描述 对于全国各大大学的男生寝室,总是有各种混乱的父子关系. 那么假设现在我们一个男生寝室有不同的 nn 个人,每个人都至多有一个“爸爸”,可以有多个“儿子”,且有且只有一个人没有“爸爸”(毕竟是 ...