【企业库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="你的属 ...
随机推荐
- pyhon MySQLdb查询出来的数据设置为字典类型
import MySQLdbimport MySQLdb.cursors cxn=MySQLdb.Connect(host='localhost',user='root',passwd='1234', ...
- Java的基本语法
Java基本语法格式 Java基本语法格式 Java中的所有程序代码都必须存在于一个类中,用class关键字定义类,在class前面可以有一些修饰符. 修饰符 class 类名{ 程序代码} 注: ① ...
- .NET 条件查询实现--类似网上商城宝贝搜索
需要实现的效果:点击表格列头:弹出一个层,用户可以输入当前列头的查询条件,点击确定之后,把该列头的查询信息显示在页面顶部,用户可以叉掉这个查询条件,恢复到查询之前的数据. 大致实现的效果图: 项目背景 ...
- Java和Android开发IDE---IntelliJ IDEA使用技巧(转)
以前一直使用的是Eclipse,听别人介绍说IDEA非常不错,也为了以后转Android studio铺垫下.就开始尝试用idea来开发. 这篇文章主要学习了idea的使用技巧. IDEA 全称 In ...
- 一、ThinkPHP的介绍
一.ThinkPHP的介绍 //了解 MVC M - Model 模型 工作:负责数据的操作 V - View 视图(模板) 工作:负责前台页面显示 编写html代码 C - Controller 控 ...
- c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow
c++ - fcgio.cpp:50: error: 'EOF' was not declared in this scope - Stack Overflow fcgio.cpp:50: error ...
- js 定义类对象
//定义类 //方式一 function A_class(arg1,arg2){ this.arg1=arg1; this.arg2=arg2; ...
- 非线性规划问题的matlab求解
函数:[x, fval] = fmincon(FUN, X0, A, B, Aeq, Beq, LB, UB, NONLCON) 返回的x:是一个向量——在取得目标函数最小时各个xi的取值: 返回的f ...
- How draw a stem -and -leaf & box-plot display by R.or Python
参考: 使用R进行数据可视化套路之-茎叶图.盒形图 step by step R 读取数据 在网上下载的2013全国各省区GDP排名(exl文件) 先 另存为 data.txt(为了方便存到D盘文件夹 ...
- VS2015操作Oracle数据需要做那些设置?
1>在oracle网上下载:ODP.NET 2> 要根据自己的oracle 数据32bit/64bit,选择下载. 3> 根据提示配置tnsnames.ora文件. # alias ...