【企业库6】【日志应用程序块】实验2:创建和使用异步Trace Listener
Lab 2: Create and Use an Asynchronous Trace Listener 实验2:创建和使用异步Trace Listener
In this lab, you will build an Asynchronous Trace Listener Wrapper to write log entries asynchronously to a disk file. Using the asynchronous wrapper changes the perceived time that it takes to log an entry. Control returns to the application faster, but the block still needs to write the log entry to its destination. You will then add this new Trace Listener to the EnoughPI application to monitor the log entries in real time. 在这个实验中,你将会建立一个异步Trace监听器包装来写异步的将日志条目写入磁盘文件。使用异步包装来改变记录日志条目的时间。操作将会更快的返回到程序,但是块依然需要将日志条目写到目的地。然后你会添加这个新的Trace Listener到EnoughPI程序来实时的监控日志条目。
To begin this exercise, open the EnoughPI.sln file located in the ex02\begin folder. 要开始这个练习,打开在ex02\begin文件夹中的EnoughPI.sln文件。
To monitor how long the log entries take 监视日志条目的长度
- Comment out or remove the Event Log Trace Listener from the BuildProgrammaticConfig method in EntryPoint.cs so you are only keeping track of the time it takes to log using your Flat File Trace Listener. 将EntryPoint.cs 文件里BuildProgrammaticConfig方法中的Event Log Trace注释掉或删除掉,这样你就只记录了是用你的Flat File Trace Listener记录的时间。
private static LoggingConfiguration BuildProgrammaticConfig()
{
// Formatter
TextFormatter formatter = new TextFormatter(@"Timestamp:
{timestamp(local)}{newline}Message:{message}{newline}Category:
{category}{newline}Priority:{priority}{newline}EventId:
{eventid}{newline}ActivityId:{property(ActivityId)}{newline}Severity:
{severity}{newline}Title:{title}{newline}");
var xmlFormatterAttributes = new NameValueCollection();
xmlFormatterAttributes["prefix"] = "x";
xmlFormatterAttributes["namespace"] = "EnoughPI/2.0";
EnoughPI.Logging.Formatters.XmlFormatter xmlFormatter =
new EnoughPI.Logging.Formatters.XmlFormatter(
xmlFormatterAttributes);
// Trace Listeners
var eventLog = new EventLog("Application", ".", "EnoughPI");
var eventLogTraceListener = new
FormattedEventLogTraceListener(eventLog, formatter);
var flatFileTraceListener =
new FlatFileTraceListener(
@"C:\Temp\trace.log",
"----------------------------------------",
"----------------------------------------",
formatter);
// Build Configuration
var config = new LoggingConfiguration();
config.AddLogSource(Category.General, SourceLevels.All, true).AddTraceListener(eventLogTraceListener);
config.AddLogSource(
Category.Trace,
SourceLevels.ActivityTracing,
true).AddTraceListener(flatFileTraceListener);
return config;
} - Select the Debug | Start Without Debugging menu command to run the application. Enter a precision of at least 300 (this will make the time improvements more apparent) and click the Calculate button. The end of the Tracing logs in C:\Temp\trace.log will tell how long it took to calculate pi. 选择 调试|开始执行(不调试)菜单命令来运行程序。输入一个至少300(这样会使用时表现的多一些)然后单击Calculate按钮。在文件 C:\Temp\trace.log的末尾就会告诉你花了多长时间来计算PI的值。
----------------------------------------
Timestamp: 7/22/2013 8:29:13 AM
Message: End Trace: Activity '67ba73cf-502c-4c3d-bc04-c2ea11c7e88f' in
method 'EnoughPI.Calc.Calculator.Calculate' at 1194567702026 ticks
(elapsed time: 3.554 seconds)
Category: Trace
Priority: 5
EventId: 1
ActivityId: 67ba73cf-502c-4c3d-bc04-c2ea11c7e88f
Severity: Stop
Title:TracerExit
----------------------------------------
----------------------------------------
Timestamp: 7/22/2013 8:29:13 AM
Message: Calculated PI to 300 digits
Category: General
Priority: 2
EventId: 100
ActivityId: 00000000-0000-0000-0000-000000000000
Severity: Information
Title:
----------------------------------------
To use a trace listener asynchronously 使用异步Trace Listener
- Use the AddAsynchronousTraceListener method in the BuildProgrammaticConfig method in EntryPoint.cs to add the flatFileTraceListener to your configuration. 在EntryPoint.cs文件的BuildProgrammaticConfig方法中调用AddAsynchronousTraceListener方法来添加到你的配置信息中。
private static LoggingConfiguration BuildProgrammaticConfig()
{
// Formatter
TextFormatter formatter = new TextFormatter("Timestamp:
{timestamp(local)}{newline}Message:
{message}{newline}Category: {category}{newline}Priority:
{priority}{newline}EventId: {eventid}{newline}ActivityId:
{property(ActivityId)}{newline}Severity:
{severity}{newline}Title:{title}{newline}"); // Trace Listeners
var flatFileTraceListener = new
FlatFileTraceListener(@"C:\Temp\trace.log",
"----------------------------------------",
"----------------------------------------",
formatter); // Build Configuration
var config = new LoggingConfiguration();
config.AddLogSource(Category.Trace, SourceLevels.ActivityTracing,
true).AddAsynchronousTraceListener(flatFileTraceListener);
config.IsTracingEnabled = true;
return config;
}Wrapping the existing FlatFileTraceListener allows you to use that Trace Listener to log messages asynchronously. This will be most useful when writing large volumes of messages to a flat file or database. 包装已有的FlatFileTraceListener使你可以使用TraceListener来异步的记录消息。这在记录大量日志消息到文件或数据库时是非常有用的。
- View the output again. It should be significantly lower now. 再次查看输出文件,现在它看起来应该向下面的内容了。
----------------------------------------
Timestamp: 7/22/2013 8:33:54 AM
Message: End Trace: Activity '00c3f38c-233c-4d46-9958-19df15242634' in
method 'EnoughPI.Calc.Calculator.Calculate' at 1195224522966 ticks
(elapsed time: 0.912 seconds)
Category: Trace
Priority: 5
EventId: 1
ActivityId: 00000000-0000-0000-0000-000000000000
Severity: Stop
Title:TracerExit
----------------------------------------
----------------------------------------
Timestamp: 7/22/2013 8:33:54 AM
Message: Calculated PI to 300 digits
Category: General
Priority: 2
EventId: 100
ActivityId: 00000000-0000-0000-0000-000000000000
Severity: Information
Title:
Note: Logging messages asynchronously can lead to messages being lost if the application terminates before the buffer is drained. Disposing the LogWriter when shutting down the application attempts to flush all asynchronous buffers.
注意:如果程序在缓冲区被排空之前就被终止了,那么异步记录消息可能会导致消息丢失。在关闭程序时处理LogWriter尝试处理所有的异步缓冲区。

To verify that you have completed the exercise correctly, you can use the solution provided in the ex02\end folder. 要证实你是否正确的完成了练习,你看以使用ex02\end文件夹中提供的解决方案。
【企业库6】【日志应用程序块】实验2:创建和使用异步Trace Listener的更多相关文章
- 【翻译】【中英对照】【企业库6】动手实验 Hands-On Lab 日志应用程序块索引页
Logging Application Block Hands-On Lab for Enterprise Library 企业库的日志应用程序块动手实验 This walkthrough shoul ...
- 使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件
这里只是说明在项目中如何配置使用微软企业库的日志组件,对数据库方面的配置请参考其他资料. 1.在项目中添加Microsoft.Practices.EnterpriseLibrary.Data.dll. ...
- 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录
在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...
- 权限管理系统源码分析(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 ...
- [EntLib]微软企业库5.0 学习之路——第一步、基本入门
话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...
- 基于微软企业库的AOP组件(含源码)
软件开发,离不开对日志的操作.日志可以帮助我们查找和检测问题,比较传统的日志是在方法执行前或后,手动调用日志代码保存.但自从AOP出现后,我们就可以避免这种繁琐但又必须要实现的方式.本文是在微软企业库 ...
- Enterprise Library 企业库
微软企业库,提供了一套日志,缓存等功能的库.可以通过NuGet安装.
- 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持
在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...
- Enterprise Library +Caliburn.Micro+WPF CM框架下使用企业库验证,验证某一个属性,整个页面的文本框都变红的原因
我用的是CM这个框架做的WPF,在用企业库的验证的时候,我用标签的方式给一个属性加了不能为空的验证,但整个页面的所有控件的外面框都变红了.原因是CM框架的绑定方式是直接X:Name="你的属 ...
随机推荐
- MVC上传文件受限制
mvc自带设置4M一下的字体可以上传,4M以上的字体需要对web.config进行设置 <system.web> <httpRuntime targetFramework=" ...
- [Usaco2009 Feb]Revamping Trails 堆优化 Dijkstra
.. 这题一眼就看出就是一个二维DP dp[i][j]表示到点i使用了j次免费边的最短距离 MD 卡SPFA.. 遂写dij. AC #include <iostream> #includ ...
- 工作日志2014-06-10(实现C语言解析XML获得查询关键字)
#include "GetInfo.h" ]; int GetInfoToWrite(char* path, char* domain,Write_t* pwrite); int ...
- 【转】几个常用的Oracle存储过程
http://blog.bossma.cn/database/some-oracle-storing-process/ 几个常用的Oracle存储过程 发布时间:2008年1月6日 / 分类:Data ...
- SQL 设计心得、逗号分隔列表
第一: 在开始编码前.主要关注数据库里要保存什么样的数据,以级最佳的数据组织方式和内在关联方式. 第二: 使用你所知的数据库特性尽可能高效的实现数据管理.如正确的索引.数据库类型.高效的select! ...
- Javascript高级程序设计学习笔记一
看完w3school的javascript的概念,有点基础,开始红皮书的路程,今晚总结前二章的心得. 第一章:javascript简介 重点是javascript的实现是由 ECMAScript(核心 ...
- github中的ssh配置
1.配置git信息 设置git的user name和email: $ git config --global user.name "tigerjibo"$ git config - ...
- XML DOM 节点
来自:w3cschool菜鸟教程 在 DOM 中,XML 文档中的每个成分都是一个节点. DOM 节点 根据 DOM,XML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档 ...
- c++栈管理库TCMalloc、jeMalloc
示例:http://blog.csdn.net/chosen0ne/article/details/9338591
- dos下遍历目录和文件的代码(主要利用for命令)
对指定路径指定文件进行遍历的程序,这里有多个批处理代码,但运行好像有些问题,大家可以根据需要选择 ===== 文件夹结构 ======================================= ...