Enterprise Library 2.0 Hands On Lab 翻译(6):日志应用程序块(三)

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:
练习3:创建并使用自定义LogFormatter
在本练习中将创建一个自定义的LogFormatter,并在应用程序中使用它。
 
第一步
打开EnoughPI.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex03\begin,并编译。
 
第二步 创建自定义LogFormatter
1 .在解决方案管理器中选择Formatters\XmlFormatter.cs文件,选择View | Code菜单命令,添加如下命名空间。
None.gif using  Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
None.gif
None.gif
using  Microsoft.Practices.EnterpriseLibrary.Logging;
None.gif
None.gif
using  Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
None.gif
None.gif
using  Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
2 .添加如下代码到XmlFormatter类中。
None.gif [ConfigurationElementType( typeof (CustomFormatterData))]
None.gif
None.gif
public   class  XmlFormatter : LogFormatter
None.gif
ExpandedBlockStart.gif
{
InBlock.gif
InBlock.gif    
private NameValueCollection Attributes = null;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif    
public XmlFormatter(NameValueCollection attributes)
InBlock.gif
ExpandedSubBlockStart.gif    
{
InBlock.gif
InBlock.gif        
this.Attributes = attributes;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif    
public override string Format(LogEntry log)
InBlock.gif
ExpandedSubBlockStart.gif    
{
InBlock.gif
InBlock.gif        
string prefix = this.Attributes["prefix"];
InBlock.gif
InBlock.gif        
string ns = this.Attributes["namespace"];
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
using (StringWriter s = new StringWriter())
InBlock.gif
ExpandedSubBlockStart.gif        
{
InBlock.gif
InBlock.gif            XmlTextWriter w 
= new XmlTextWriter(s);
InBlock.gif
InBlock.gif            w.Formatting 
= Formatting.Indented;
InBlock.gif
InBlock.gif            w.Indentation 
= 2;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            w.WriteStartDocument(
true);
InBlock.gif
InBlock.gif            w.WriteStartElement(prefix, 
"logEntry", ns);
InBlock.gif
InBlock.gif            w.WriteAttributeString(
"Priority", ns,
InBlock.gif
InBlock.gif                log.Priority.ToString(CultureInfo.InvariantCulture));
InBlock.gif
InBlock.gif            w.WriteElementString(
"Timestamp", ns, log.TimeStampString);
InBlock.gif
InBlock.gif            w.WriteElementString(
"Message", ns, log.Message);
InBlock.gif
InBlock.gif            w.WriteElementString(
"EventId", ns,
InBlock.gif
InBlock.gif                log.EventId.ToString(CultureInfo.InvariantCulture));
InBlock.gif
InBlock.gif            w.WriteElementString(
"Severity", ns, log.Severity.ToString());
InBlock.gif
InBlock.gif            w.WriteElementString(
"Title", ns, log.Title);
InBlock.gif
InBlock.gif            w.WriteElementString(
"Machine", ns, log.MachineName);
InBlock.gif
InBlock.gif            w.WriteElementString(
"AppDomain", ns, log.AppDomainName);
InBlock.gif
InBlock.gif            w.WriteElementString(
"ProcessId", ns, log.ProcessId);
InBlock.gif
InBlock.gif            w.WriteElementString(
"ProcessName", ns, log.ProcessName);
InBlock.gif
InBlock.gif            w.WriteElementString(
"Win32ThreadId", ns, log.Win32ThreadId);
InBlock.gif
InBlock.gif            w.WriteElementString(
"ThreadName", ns, log.ManagedThreadName);
InBlock.gif
InBlock.gif            w.WriteEndElement();
InBlock.gif
InBlock.gif            w.WriteEndDocument();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
return s.ToString();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}
日志项将被格式化为 XML格式,并且它期望接收两个参数 prefixnamespace
3 .选择Build | Build Solution编译整个解决方案。
 
第三步 使用自定义LogFormatter
1 .在解决方案管理器中选择项目EnoughPI的配置文件App.config文件,选择View | Open With…菜单命令,选择Enterprise Library Configuration并单击OK按钮。
2 .选中Logging Application Block | Formatters节点,选择Action | New | Custom Formatter菜单命令,并设置属性Name的为Xml Formatter
3 .选择Type属性,单击ellipses显示Type Selector对话框。
4 .从程序集EnoughPI.Logging中选择XmlFormatter类并单击OK按钮。
Type Selector列表中的类,来自于与Enterprise Library Configuration配置工具在同一目录下的程序集,它们继承于基类LogFormatter,并且有一个值为CustomTraceListenerData的特性ConfigurationElementType
5 .选择Attributes属性并单击ellipses显示EditableKeyValue Collection Editor
6 .添加如下键值对
Key = prefix, Value = x
Key = namespace, Value = EnoughPI/2.0
并单击OK按钮。
还记得在类XmlFormatter中期望接受的两个参数prefixnamespace
7 .选择Logging Application Block | Trace Listeners | Custom TraceListener节点,并设置属性FormatterXml Formatter
8 .选择File | Save All保存配置,并关闭Enterprise Library Configuration工具。
9 .选择Debug | Start Without Debugging菜单命令并运行应用程序,EnoughPI程序用来计算∏的精度。在NumericUpDown控件中输入你希望的精度并点击Calculate按钮。可以看到日志项显示在一个控制台窗口中。
完成后的解决方案代码如C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Logging\exercises\ex03\end所示。
 
注意根据Hands On Lab给出的时间建议,做完以上三个练习的时间应该为30分钟。
 

更多Enterprise Library的文章请参考《Enterprise Library系列文章











本文转自lihuijun51CTO博客,原文链接:http://blog.51cto.com/terrylee/67636 ,如需转载请自行联系原作者



相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
14天前
|
机器学习/深度学习 存储 监控
Elasticsearch 在日志分析中的应用
【9月更文第2天】随着数字化转型的推进,日志数据的重要性日益凸显。日志不仅记录了系统的运行状态,还提供了宝贵的洞察,帮助企业改进产品质量、优化用户体验以及加强安全防护。Elasticsearch 作为一个分布式搜索和分析引擎,因其出色的性能和灵活性,成为了日志分析领域的首选工具之一。本文将探讨如何使用 Elasticsearch 作为日志分析平台的核心组件,并详细介绍 ELK(Elasticsearch, Logstash, Kibana)栈的搭建和配置流程。
52 4
|
18天前
|
Java API 开发者
你的应用是不是只有service_stdout.log?
本文记录了logback-spring.xml文件不生效问题的整体排查思路。
|
21天前
|
Java 应用服务中间件 HSF
Java应用结构规范问题之配置Logback以仅记录错误级别的日志到一个滚动文件中的问题如何解决
Java应用结构规范问题之配置Logback以仅记录错误级别的日志到一个滚动文件中的问题如何解决
|
21天前
|
Java 应用服务中间件 HSF
Java应用结构规范问题之配置Logback以在控制台输出日志的问题如何解决
Java应用结构规范问题之配置Logback以在控制台输出日志的问题如何解决
|
21天前
|
Java 应用服务中间件 HSF
Java应用结构规范问题之AllLoggers接口获取异常日志的Logger实例的问题如何解决
Java应用结构规范问题之AllLoggers接口获取异常日志的Logger实例的问题如何解决
|
16天前
|
数据库 Java 监控
Struts 2 日志管理化身神秘魔法师,洞察应用运行乾坤,演绎奇幻篇章!
【8月更文挑战第31天】在软件开发中,了解应用运行状况至关重要。日志管理作为 Struts 2 应用的关键组件,记录着每个动作和决策,如同监控摄像头,帮助我们迅速定位问题、分析性能和使用情况,为优化提供依据。Struts 2 支持多种日志框架(如 Log4j、Logback),便于配置日志级别、格式和输出位置。通过在 Action 类中添加日志记录,我们能在开发过程中获取详细信息,及时发现并解决问题。合理配置日志不仅有助于调试,还能分析用户行为,提升应用性能和稳定性。
33 0
|
21天前
|
监控 Java Serverless
美团 Flink 大作业部署问题之想在Serverless平台上实时查看Spring Boot应用的日志要怎么操作
美团 Flink 大作业部署问题之想在Serverless平台上实时查看Spring Boot应用的日志要怎么操作
|
22天前
|
存储 大数据 索引
【Azure Contianer Apps】在云上使用容器应用时收集日志遇见延迟问题
【Azure Contianer Apps】在云上使用容器应用时收集日志遇见延迟问题
|
22天前
|
存储
【Azure Log A workspace】Azure上很多应用日志收集到Log A workspace后如何来分别各自的占比呢?
【Azure Log A workspace】Azure上很多应用日志收集到Log A workspace后如何来分别各自的占比呢?
|
22天前
|
存储 Java Spring
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?