欢迎光临

木子屋

标题: Enterprise Library 5.0之Logging Application Block使用示例

作者: dnawo

日期: 2012-09-01 10:46 AM

分类: Win编程

内容: 1.生成配置文件
打开Enterprise Library Configuration(EntLibConfig.exe)，配置如下：
1.1 界面说明
①.Categories：日志分类，每个LogEntry至少关联一个分类，Logging Application Block允许添加多个分类，每个分类可添加多个Listeners；
②.Special Categories：特殊分类，不用和LogEntry关联，配置好就会生效。
·All Events：记录全部事件日志；
·Unprocessed Category：记录没有合适分类的日志，开启All Events时本分类失效；
·Logging Error & Warnings：记录Logging Application Block错误日志；
③.Logging Filters：过滤器；
④.Logging Target Listeners：日志位置保存，有Database、E-mail、Event Log、Flat File、Message Queuing、Rolling Flat File、System Diagnostics、WMI、XML；
⑤.Log Message Formatters：日志格式化，有Text Formatter和Binary Log Message Formatter ；
1.2 生成app.config文件
复制内容到剪贴板 程序代码<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" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="c:\trace.log" formatter="Text Formatter" filter="All" />
<add name="Email Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
toAddress="270250392@qq.com" fromAddress="597421489@qq.com"
subjectLineStarter="ServerError" smtpServer="smtp.qq.com"
formatter="Text Formatter" authenticationMode="UserNameAndPassword"
userName="597421489" password="666666" filter="Error" />
</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}{newline}
Message: {message}{newline}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Flat File Trace Listener" />
<add name="Email 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" />
</specialSources>
</loggingConfiguration>
</configuration>
2.Logging Application Block使用示例
2.1 添加引用
复制内容到剪贴板 程序代码Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
2.2 示例代码
复制内容到剪贴板 程序代码using System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
LogEntry entry = new LogEntry();
entry.Categories.Add("General");
//记录日志
entry.Message = "发生错误，记录文本日志。";
Logger.Write(entry);
//记录日志
entry.Message = "发生严重错误，记录文本日志，邮件提醒管理员。";
entry.Severity = TraceEventType.Error;
Logger.Write(entry);
Console.WriteLine("ok");
Console.ReadKey();
}
}
}
2.3 执行结果

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.566 ms

返回

标题: Enterprise Library 5.0之Logging Application Block使用示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.566 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.598 ms

返回