使用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 ...
随机推荐
- oracle查询时间段内的数据
select * from persons o where trunc(o.create_date) = to_date('2018-07-30','yyyy-mm-dd') minus 对比数据完 ...
- BigDecimal运算(加、减、乘、除)
public class BigDecimalOperation { private BigDecimalOperation(){ } public static BigDecimal add(dou ...
- react.js中实现tab吸顶效果问题
在react项目开发中有一个需求是,页面滚动到tab所在位置时,tab要固定在顶部. 实现的思路其实很简单,就是判断当滚动距离scrollTop大于tab距离页面顶部距离offsetTop时,将tab ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--L-用来作弊的药水
链接:https://www.nowcoder.com/acm/contest/90/L 来源:牛客网 1.题目描述 -- 在一个风雨交加的夜晚,来自异世界的不愿透露姓名的TMK同学获得了两种超强药水 ...
- (Linux 日常命令)[20171225]
目的:记录Linux日常所用命令 [20171222]Linux环境下查看硬件组件型号 cat /proc/cpuinfo及lspci 查看CPU [root@t-redhat- ~]# cat /p ...
- ubuntu系统下的docker
官网:https://www.docker.com/ 相关资料:1.Docker入门教程 http://dockone.io/article/1112.Docker_百度百科 http://baike ...
- vsftpd文件服务参数汇总和虚拟用户使用
FTP文件传输协议 FTP协议特点 基于C/S结构 双通道协议:数据和命令连接 数据传输格式:二进制(默认)和文本(w文本格式会修改文件内容) 两种模式:服务器角度 主动(PORT style):服务 ...
- 微信小程序navigator无法跳转情况
情况有三种 跳转的页面没有在app.json中注册 跳转的路径不正确 以上两种在命令行(console)中都会提示 跳转的页面在TabBar中,需要将open-type属性是设置为switchTab
- nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory)
解决办法: nginx nginx -s reload
- 【c学习-12】
/*枚举*/ #include void enumFunction(){ enum enum_var{"a","b",1,2}; enum{"c&qu ...