儘管 EL4 的 Logging Application Block(以下簡稱 LAB)已經提供很多種 trace listeners,但有時候還是無法完全符合我們的需要,例如上一篇提到的,Email Trace Listener 並未支援 SMTP 伺服器身份驗證的功能。此時就得自己寫一個 log 監聽器(sink)了。
在撰寫自訂的 log 監聽器時,有幾個關鍵:
練習步驟分成兩個部分,一是設計自訂的 log 監聽器,這裡會將自訂監聽器類別包在一個名為 MyListener.dll 的組件裡,方便應用程式專案引用。第二部分就是說明應用程式專案如何使用自訂的 log 監聽器;簡單起見,我會使用上一篇的網站範例來說明,以省略那些「建立新專案、編輯組態檔...」的瑣碎步驟。
Here we go!
Part I: 建立自訂 log 監聽器
註:簡單起見,這裡我只有讓 SMTP 伺服器名稱和傳輸埠這兩個設定值取自外部組態檔,其實其他 mail 設定資訊也都可以如法泡製。
Part II:在應用程式中使用自訂 log 監聽器
MyListener 組件建立成功後,接著就可以在我們的應用程是專案中使用。這裡沿用上一篇的 Web 網站範例,請依下列步驟操作:
從最簡單的 EventLog、不斷翻滾的可重複寫入的 Rolling Flat File Trace Listener、到 Email Trace Listener,再到自訂監聽器的撰寫,不難發現,Logging Application Block 的主角就是 trace listener 物件,這是一種以 observer pattern 為核心概念所設計成的框架。
還缺什麼呢?比較常用的可能就剩下 Database Trace Listener 吧。不過,寫完這三部曲,已經有點懶了 @_@ Logging Application Block 的入門教學我想就先寫到這裡吧。Happy coding :)
相關文章
在撰寫自訂的 log 監聽器時,有幾個關鍵:
- 你的監聽器類別須繼承自 LAB 的 CustomerTraceListener 類別。
- 你的監聽器類別須套用 ConfigurationElementTypeAttribute,並指定組態型別為 CustomTraceListenerData(這個部分在看到程式碼時就明白了)。
- 改寫必要的 methods。你至少要改寫 Write 和 WriteLine 方法。
練習步驟分成兩個部分,一是設計自訂的 log 監聽器,這裡會將自訂監聽器類別包在一個名為 MyListener.dll 的組件裡,方便應用程式專案引用。第二部分就是說明應用程式專案如何使用自訂的 log 監聽器;簡單起見,我會使用上一篇的網站範例來說明,以省略那些「建立新專案、編輯組態檔...」的瑣碎步驟。
Here we go!
Part I: 建立自訂 log 監聽器
- 建立一個 Class Library 專案,命名為「MyListener」。
- 加入下列 .NET 組件參考:
Enterprise Library Shared Library(組件檔名是 Microsoft.Practices.EnterpriseLibrary.Common.dll)
Enterprise Library Logging Application Block
System.Configuration
- 刪除預設產生的 Class1.cs,再加入一個新類別:SmtpTraceListener.cs。
- 此類別須繼承自 CustomerTraceListene,並改寫三個方法:TraceData、Write、和 WriteLine。參考程式碼列表 1。
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Net;
6: using System.Net.Mail;
7: using System.Diagnostics;
8: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
9: using Microsoft.Practices.EnterpriseLibrary.Logging;
10: using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
11: using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
12:
13: namespace MyListener
14: {
15: [ConfigurationElementType(typeof(CustomTraceListenerData))]
16: public class SmtpTraceListener : CustomTraceListener
17: {
18: public SmtpTraceListener()
19: {
20: }
21:
22: public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
23: {
24: if (data is LogEntry && this.Formatter != null)
25: {
26: Write(this.Formatter.Format(data as LogEntry));
27: }
28: else
29: {
30: Write(data.ToString());
31: }
32: }
33:
34: public override void Write(string message)
35: {
36: SendMail(message);
37: }
38:
39: public override void WriteLine(string message)
40: {
41: SendMail(message);
42: }
43:
44: public void SendMail(string message)
45: {
46: string smtpServer = this.Attributes["SmtpServer"];
47: int smtpPort = Convert.ToInt32(this.Attributes["SmtpPort"]);
48:
49: MailMessage msg = new MailMessage();
50: SmtpClient smtp = new SmtpClient(smtpServer, smtpPort);
51: msg.From = new MailAddress("YourEmail@gmail.com");
52: msg.To.Add("WillGates@michaelsoft.com");
53: msg.IsBodyHtml = false;
54: msg.Body = message;
55: msg.Subject = "My Email Trace Listener Log 訊息" ;
56:
57: smtp.UseDefaultCredentials = false;
58: smtp.EnableSsl = true;
59: smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
60: NetworkCredential userCred = new NetworkCredential("[帳號]", "[密碼]");
61: smtp.Credentials = userCred;
62:
63: smtp.Send(msg);
64: }
65: }
66: }
程式碼其實還蠻簡單的,只需要特別注意兩個地方,一個是前面提過的,SmtpTraceListener 的父類別,以及套用至此類別的 attribute。另一個地方是第 46~47 行,這兩行程式碼是從這個物件本身的 Attributes 屬性中取出 SMTP 伺服器名稱和傳輸埠。那麼 Attributes 裡面的值又是哪裡來的呢?答案是:應用程式的組態檔。這個部分在接下來的 Part II 練習會看得比較清楚。註:簡單起見,這裡我只有讓 SMTP 伺服器名稱和傳輸埠這兩個設定值取自外部組態檔,其實其他 mail 設定資訊也都可以如法泡製。
Part II:在應用程式中使用自訂 log 監聽器
MyListener 組件建立成功後,接著就可以在我們的應用程是專案中使用。這裡沿用上一篇的 Web 網站範例,請依下列步驟操作:
- 加入組件參考:MyListener.dll。
- 用 Enterprise Library 組態檔編輯器開啟 web.config(在 Solution Explorer 中的 web.config 上點右鍵,選「Edit Enterprise Library Configuration」)。
- 把原本的 Email Trace Listener 項目都移除掉。(此步驟非必要)
- 加入自訂監聽器:在節點 Logging Application Block > Trace Listeners 上點右鍵,選 New > Custom Trace Listener。然後修改其屬性,將 Name 改為 "My Email Trace Listener"(名字隨你訂),並設定 Type 屬性以指定監聽器的型別。參考下圖:

按 Load from File 鈕,直接挑選網站的 bin 目錄下的 MyListener.dll:

選好之後,點一下畫面上的 SmtpTraceListener,按 OK:

- 一樣是修改監聽器的屬性,這次要設定 Attributes 屬性。沒錯!就是 Part I 程式碼的第 46~47 行要取的屬性。所以這裡要加入兩個屬性:SmtpServer 和 SmtpPort。參考下圖:


- 在節點 Logging Application Block > Category Sources > General 上點右鍵,選 New > Trace Listener Reference。再設定其 ReferencedTraceListener 屬性為 "My Email Trace Listener"。
- 設定完成後,請注意不管 Visual Studio 編輯器顯示此 web.config 是否有修改過,最好都按一下存檔。存檔完成後,把 Web.config 關閉,再以 Visual Studio 預設的編輯器開啟,以檢視其文字內容,看看剛才的動作到底加了哪些元素。
- 原本的程式碼都不用動,Build 網站之後直接瀏覽 Default.aspx,然後檢查你在程式碼列表 1 的第 52 行指定的信箱,看看有沒有收到標題為「My Email Trace Listener Log 訊息」的信件。
從最簡單的 EventLog、
還缺什麼呢?比較常用的可能就剩下 Database Trace Listener 吧。不過,寫完這三部曲,已經有點懶了 @_@ Logging Application Block 的入門教學我想就先寫到這裡吧。Happy coding :)
相關文章
沒有留言: