Enterprise Library: Logging and Instrumentation Application Block-Email接收池设计向导篇

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,并设置相应属性的属性值,如下图所示:
Enterprise_Logging_EmailSink.jpg


另外,在
Categories节点下添加新的Category并命名为EmailNotification。然后,在EmailNotification目录下添加新的Destination,并设置相应的属性。如下图所示:
Enterprise_Logging_ConfigurateEmailSink.jpg

 

当然你也可以直接现有的Category下添加上述Destination

 

2. Demo代码及其界面

1Code 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;

          }

}

 

2Demo应用程序界面
Enterprise_Logging_DemoEmailSink.jpg

3. 应注意的问题及其改进

EmailSink调用System.Web.Mail,该Class对调用方有诸多要求,如需要调用本地的SMTP ServiceCDO.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 LibraryLogging 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

 

 

你可能感兴趣的:(application)