Enterprise Library: Logging and Instrumentation Application Block-Email接收池设计向导篇
Written by: Rickie Lee (rickieleemail#yahoo.com)
My blog:http://www.cnblogs.com/rickie
Enterprise Library Quick Start中演示了使用Logging and Instrumentation Application Block记录日志信息到Application事件日志和文本文件。《Enterprise Library: Logging and Instrumentation Application Block数据库接收池(Database Sink)设计向导篇》(作者:Rickie Lee - rickieleemail#yahoo.com)演示将日志信息写入Database。这里,演示如何将日志信息写入Email并发送。
1. 设置配置文件
使用Configuration Console配置工具,在Sinks节点下添加Email Sink,并设置相应属性的属性值,如下图所示:
另外,在Categories节点下添加新的Category并命名为EmailNotification。然后,在EmailNotification目录下添加新的Destination,并设置相应的属性。如下图所示:
当然你也可以直接现有的Category下添加上述Destination。
2. Demo代码及其界面
(1)Code Snippet
与《Enterprise Library: Logging and Instrumentation Application Block数据库接收池(Database Sink)设计向导篇》中将日志信息写入Database基本一致,只是简单调整了LogEntry对象的Category属性值。
private void btnEmailNotification_Click(object sender, System.EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
LogEntry log = new LogEntry();
log.Message = "Hello, Rickie.";
log.Priority = 1;
log.EventId = 100;
log.Severity = Severity.Information;
log.Title = "Demo on Logging and Instrumentation Application Block";
log.Category = "EmailNotification";
Logger.Write(log);
txtResults.Text += log.Message + Environment.NewLine;
}
finally
{
Cursor.Current = Cursors.Default;
}
}
(2)Demo应用程序界面
3. 应注意的问题及其改进
EmailSink调用System.Web.Mail,该Class对调用方有诸多要求,如需要调用本地的SMTP Service和CDO.Message对象等。
因此,在实际的业务应用开发中,推荐编写自定义的Sink,调用Web Services或采用.Net Remoting技术调用Remote Objects。
Custom Sink如下所示:
[C#]
public class CustomEmailSink : LogSink, IConfigurationProvider
[Visual Basic]
Public Class CustomEmailSink
Inherits LogSink
Implements IConfigurationProvider
定制的sink需要实现IConfigurationProvider接口,并继承LogSink抽象类。另外需要增加SendMessageCore方法到定制sink类中,确保实现必要的动作来记录日志信息到定制的sink。
***
作者:Rickie Lee (rickieleemail#yahoo.com)
本文参考Microsoft Enterprise Library中Logging and Instrumentation Application Block文档。
References:
1. Microsoft Enterprise Library: Logging and Instrumentation Application Block.
2. Rickie, Microsoft patterns & practices Enterprise Library January 2005 [中文稿], http://www.cnblogs.com/rickie/archive/2005/01/30/99443.html
3. Rickie, Enterprise Library released! http://www.cnblogs.com/rickie/archive/2005/01/29/99106.html