【企业库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="你的属 ...
随机推荐
- PHPEXCEL导入小技巧
在导入excel的时候,单元格格式和公式经常让导入不顺畅.注意phpexcel文档说明,基本上就可以很顺利的导入. 1.忽略单元格格格式,并导入xls.xlsx两种格式 $objReader = PH ...
- Java动态代理机制——Cglib
上一篇说过JDK动态代理机制,只能代理实现了接口的类,这就造成了限制.对于没有实现接口的类,我们可以用Cglib动态代理机制来实现. Cglib是针对类生成代理,主要是对用户类生成一个子类.因为有继承 ...
- python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb
在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...
- 关于scanf("%c",&ch)直接跳过的问题
有时候scanf("%c",&ch)本应该阻塞等待用户输入一个char型数据的,但为什么会跳过呢? 例:在该程序段中, int year; printf(" ...
- SSD的优势
谈过SSD的发展历史后,现在我们来讲解下SSD相比传统HDD(机械硬盘)的优势. 相信很多读者只要有听说过SSD,必定都会听到对SSD优点的一个字总结:快! 但这一个字要如何去理解呢?很多人可能还不太 ...
- JSONP跨域的原理解析及其实现介绍
JSONP跨域的原理解析及其实现介绍 作者: 字体:[增加 减小] 类型:转载 时间:2014-03-22 JSONP跨域GET请求是一个常用的解决方案,下面我们来看一下JSONP跨域是如何实现的,并 ...
- SRM 585 DIV 1 总结
题意:给你一棵高度为H的完全二叉树的路,问最少需要多少辆车把这路走完,车子不能返回. 那么最优的方案就是从小到上一层层的走完,就很容易地可以得到一种递推,需要注意的就是dp[0] = 1 #incl ...
- OOP中的多态
尽管一直在说OOP,但说实话还不是真正的理解,面向对象的三个基本特性继承.封装.多态,前两个性质曾经 有接触听的比較多还好理解,以下主要介绍一下第三个特性--多态. 1. 定义 同一操作作用于 ...
- android专栏
Android之Activity(8) Android之Adapter(1) Android之ContentProvider(1) Android之Handler(4) Android之JSON(2) ...
- POJ 3169 Layout (图论-差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6574 Accepted: 3177 Descriptio ...