EnterpriseLibrary4 自己封装程序集实现log打印
注意:1)要引用响应的程序集,必须是41的
2)配置文件
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
using Microsoft.Practices.EnterpriseLibrary.Logging.Instrumentation;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
namespace Core
{
public class MicroLogger
{
// Fields
private const int DefaultEventId = ;
private const int DefaultPriority = -;
private const TraceEventType DefaultSeverity = TraceEventType.Information;
private const string DefaultTitle = "";
private static readonly ICollection<string> emptyCategoriesList = new List<string>();
private static LogWriterFactory factory = new LogWriterFactory(ConfigurationSourceFactory.Create());
private static object sync = new object();
private static volatile LogWriter writer; //第三步
public static void Write(LogEntry log)
{
Writer.Write(log);
}
//第一步
public static void Write(object message)
{
Write(message, emptyCategoriesList, -, , TraceEventType.Information, "", null);
} #region
public static void Write(object message, ICollection<string> categories)
{
Write(message, categories, -, , TraceEventType.Information, "", null);
} //第二步
public static void Write(object message, ICollection<string> categories, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)
{
LogEntry log = new LogEntry
{
Message = message.ToString(),
Categories = categories,
Priority = priority,
EventId = eventId,
Severity = severity,
Title = title,
ExtendedProperties = properties
};
Write(log);
} //第四步
// Properties
public static LogWriter Writer
{
get
{
if (writer == null)
{
lock (sync)
{
if (writer == null)
{
try
{
writer = factory.Create();
}
catch (ConfigurationErrorsException exception)
{
TryLogConfigurationFailure(exception);
throw;
}
}
}
}
return writer;
}
}
}
}
对应的配置文件,可以通用
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="loggingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections> <loggingConfiguration name="Logging Application Block"
tracingEnabled="true"
defaultCategory="General"
logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="log\JobExecute.log"
footer="----------------------------------------"
formatter="Text Formatter"
header="----------------------------------------"
rollFileExistsBehavior="Increment"
rollInterval="Day"
rollSizeKB="0"
timeStampPattern="yyyy-MM-dd"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Rolling Flat File Trace Listener JobExecute" /> <add fileName="log\error.log"
footer="----------------------------------------"
formatter="Text Formatter"
header="----------------------------------------"
rollFileExistsBehavior="Increment"
rollInterval="Day"
rollSizeKB="0"
timeStampPattern="yyyy-MM-dd"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Rolling Flat File Trace Listener Error" /> <add fileName="log\returnError.log"
footer="----------------------------------------"
formatter="Text Formatter"
header="----------------------------------------"
rollFileExistsBehavior="Increment"
rollInterval="Day"
rollSizeKB="0"
timeStampPattern="yyyy-MM-dd"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Rolling Flat File Trace Listener ReturnError" /> <add fileName="log\other.log"
footer="----------------------------------------"
formatter="Text Formatter"
header="----------------------------------------"
rollFileExistsBehavior="Increment"
rollInterval="Day"
rollSizeKB="0"
timeStampPattern="yyyy-MM-dd"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Rolling Flat File Trace Listener Other" /> <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All"
type="YTO.Framework.Logging.Database.DatabaseTraceListener, YTO.Framework.Logging.Database"
initializeData=""
formatter="Text Formatter"
name="SqlServer Trace Listener"/>
</listeners> <formatters>
<add template="Timestamp: {timestamp(local)} Message: {message} Category: {category} Priority: {priority} EventId: {eventid} Severity: {severity} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties: {dictionary({key} - {value} )}"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Text Formatter" />
</formatters> <categorySources>
<add switchValue="All"
name="Exception">
<listeners>
<add name="Rolling Flat File Trace Listener Error" />
</listeners>
</add>
<add switchValue="All"
name="JobExecute">
<listeners>
<add name="Rolling Flat File Trace Listener JobExecute" />
</listeners>
</add>
<add switchValue="All"
name="ReturnError">
<listeners>
<add name="Rolling Flat File Trace Listener ReturnError" />
</listeners>
</add>
</categorySources> <specialSources>
<allEvents switchValue="All"
name="All Events" /> <notProcessed switchValue="All"
name="Unprocessed Category">
<listeners>
<add name="Rolling Flat File Trace Listener Other" />
</listeners>
</notProcessed> <errors switchValue="All"
name="Logging Errors & Warnings">
<listeners>
<add name="Rolling Flat File Trace Listener Error" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
</configuration>
参考链接:
http://blog.csdn.net/ghostbear/article/details/8194460
http://tech.ddvip.com/2008-10/122369719476816.html
http://www.cnblogs.com/huangcong/archive/2010/05/31/1748672.html
EnterpriseLibrary4 自己封装程序集实现log打印的更多相关文章
- C#封装程序集自定义类方法注释提示
一.为什么使用封装程序集: 在很多分布式应用程序开发中,针对每一种功能可能条用的接口不一样,往往习惯将需要被调用的接口,封装成DLL给调用方应用后使用,这样既规范了调用的方式,又避免了调用出现参数请求 ...
- JS高级群的日常!写一个从10到0的倒计时,用console.log打印,不可以用 setInterval!本来说好的研究avalonJS最后演变成了看着大神在那边互相比拼实力。。
JS高级群的日常!写一个从10到0的倒计时,用console.log打印,不可以用 setInterval!本来说好的研究avalonJS最后演变成了看着大神在那边互相比拼实力.. 小森执行一 ...
- 关于Debug下的Log打印问题
在项目中为了调试经常会用到Log打印,比如打印当前方法__func__, 对象,地址等等,所以项目最后每次运行调试控制台满满的都是打印日志,到release发布的时候,显然不太合适,这里其实可以用一个 ...
- C#封装程序集属性方法注释说明
一.使用封装程序集好处: 在很多分布式应用程序开发中,针对每一种功能可能条用的接口不一样,往往习惯将需要被调用的接口,封装成DLL给调用方应用后使用,这样既规范了调用的方式,又避免了调用出现参数请求方 ...
- javascript篇-console.log()打印object却显示为字符串[object object]
console.log打印对象遇到的一个问题,如下截图 打印结果与预期不符,原因是因为字符串‘a’和对象object拼接在一起,拼成了一个字符串
- tiny4412 串口驱动分析七 --- log打印的几个阶段之内核启动阶段(earlyprintk)
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- 关于console.log() 打印得引用类型得数据得相关问题
console.log()打印出来得是这个引用类型最终得结果,而不是在打印得时候当前得值 ,b:} console.log(json) json.a = ; 如上 ,打印得将是 {a:3,b:2} ...
- xcode中自定义log打印
打印内容包括 在哪个文件中 ? 在哪个方法中? 将要执行什么操作? // 此打印实现前提: // 1.在.pch文件中实现自定义log打印方法,log名换为LCLog // 2.定义一个宏obje ...
- 基于 Android NDK 的学习之旅-----JNI LOG 打印
程序都是调出来的. 下面我介绍下JNI层的log打印方法的使用,类似与Android sdk提供的log 1.Android 应用层 MainActivity.java 主要功能代码 a) ...
随机推荐
- swiftlint升级
~/Opal/iOS/App (mike/lyrics) $ which brew/usr/local/bin/brew~/Opal/iOS/App (mike/lyrics) $ cd /usr/l ...
- 【如何快速的开发一个完整的iOS直播app】(推流篇)
前言 在看这篇之前,如果您还不了解直播原理,请查看这篇文章如何快速的开发一个完整的iOS直播app(原理篇) 开发一款直播app,肯定需要流媒体服务器,本篇主要讲解直播中流媒体服务器搭建,并且讲解了如 ...
- lrzsz在CentOS7的安装
在超级用户下打一句命令: yum install lrzsz 或者,在普通用户打一句命令,需要输入超级用户密码: sudo yum install lrzsz 然后使用Xshell 5建立连接即可
- Echart多图联动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- php 应用 cpu 100% 调试方法
找出进程占用cpu高的原因. 进程占用cpu高,一般是由于进程长时间占用cpu,又没有主动释放占用.如果想主动释放cpu,可以调用sleep.在写程序的时候,尤其要注意while 等循环的地方. 找出 ...
- freemarker实例2
下面演示一个简单的使用项目过程:1. 使用myeclipse创建一个web项目testFM2. 把下载到的jar包(freemarker-2.3.9.jar)放到/WebRoot/WEB-INF/li ...
- 查看Nginx、apache、MySQL和PHP的编译参数
1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# cat /usr/local/apache/build/config.nice3.p ...
- useradd/du/df/passwd/usermod命令
一.useradd命令 useradd命令-M -u -s -g 常用 -c:加上备注文字,备注文字保存在passwd的备注栏中. -d:指定用户登入时的启始目录. -D:变更预设值.(修改默认配置 ...
- 最长公共上升子序列(LICS) 模板
void LICS() { ;i<=n;i++) { ; ;j<=n;j++) { if (a[i]==b[j]) f[i][j]=ma+; ][j]; ][j]>ma) ma=f[ ...
- RHEL 6.3安装(超级详细图解教程)[转载]
附:RHEL6.3下载地址 32位:http://rhel.ieesee.net/uingei/rhel-server-6.3-i386-dvd.iso 64位:http://rhel.iee ...