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

本文介绍如何创建并使用自定义的LogFormatter来格式化日志为XML格式。通过实例演示了从创建到编译再到配置的全过程。

练习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.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
private NameValueCollection Attributes = null;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif    
public XmlFormatter(NameValueCollection attributes)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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.gifContractedSubBlock.gif    
dot.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.gifContractedSubBlock.gif        
dot.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系列文章

转载于:https://www.cnblogs.com/Terrylee/archive/2006/10/06/Logging_Application_Block_HandsOnLab_Part3.html

一、数据采集层:多源人脸数据获取 该层负责从不同设备 / 渠道采集人脸原始数据,为后续模型训练与识别提供基础样本,核心功能包括: 1. 多设备适配采集 实时摄像头采集: 调用计算机内置摄像头(或外接 USB 摄像头),通过OpenCV的VideoCapture接口实时捕获视频流,支持手动触发 “拍照”(按指定快捷键如Space)或自动定时采集(如每 2 秒采集 1 张),采集时自动框选人脸区域(通过Haar级联分类器初步定位),确保样本聚焦人脸。 支持采集参数配置:可设置采集分辨率(如 640×480、1280×720)、图像格式(JPG/PNG)、单用户采集数量(如默认采集 20 张,确保样本多样性),采集过程中实时显示 “已采集数量 / 目标数量”,避免样本不足。 本地图像 / 视频导入: 支持批量导入本地人脸图像文件(支持 JPG、PNG、BMP 格式),自动过滤非图像文件;导入视频文件(MP4、AVI 格式)时,可按 “固定帧间隔”(如每 10 帧提取 1 张图像)或 “手动选择帧” 提取人脸样本,适用于无实时摄像头场景。 数据集对接: 支持接入公开人脸数据集(如 LFW、ORL),通过预设脚本自动读取数据集目录结构(按 “用户 ID - 样本图像” 分类),快速构建训练样本库,无需手动采集,降低系统开发与测试成本。 2. 采集过程辅助功能 人脸有效性校验:采集时通过OpenCV的Haar级联分类器(或MTCNN轻量级模型)实时检测图像中是否包含人脸,若未检测到人脸(如遮挡、侧脸角度过大),则弹窗提示 “未识别到人脸,请调整姿态”,避免无效样本存入。 样本标签管理:采集时需为每个样本绑定 “用户标签”(如姓名、ID 号),支持手动输入标签或从 Excel 名单批量导入标签(按 “标签 - 采集数量” 对应),采集完成后自动按 “标签 - 序号” 命名文件(如 “张
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值