Enterprise Library 3.0 — Logging Application Block 記錄日誌至資料庫

來源:互聯網
上載者:User
Enterprise Library 3.0 -- Logging Application Block 記錄日誌至資料庫

    安裝Enterprise Library 3.0及其源碼,然後編譯源碼,在EntLib3Src\App Blocks\bin檔案夾中產生37個dll檔案,包括了Enterprise Library 3.0的所有“Block”,EntLibConfig.exe是可視化設定檔改寫工具,正因為這個可視化組態工具,我才沒有選用Log4Net而選用了Logging做為日誌記錄組件,也才有了今天的這篇隨筆。

    本文說一下如何使用Logging把日誌記錄到資料庫中,對於記錄至其它檔案/媒體介質不做敘述。

1.LoggingDatabase.sql

LoggingDatabase.sql檔案在EntLib3Src\App Blocks\Src\Logging\TraceListeners\Database\Scripts下,如果要把日誌記錄在資料庫,那麼請先使用此sql指令碼建立資料庫。

執行此sql指令碼後,產生三個表:Category、CategoryLog、Log,四個預存程序:AddCategory、ClearLogs、InsertCategoryLog、WriteLog。

2.建立你的web項目或windows項目,將EntLib3Src\App Blocks\bin的dll(37個dll不是全部用到,我偷懶給全部複製過來了)複製到建立的項目的bin中,同時將EntLibConfig.exe檔案也複製過來吧。

3.啟動EntLibConfig.exe,並開啟你的項目的web.config或app.config,如:

 

4.將Connection Strings節點右鍵,new-> Connection String,連接字串改為第1步中建立的資料庫的連接字串,如:Database= Logging;Server=ZT-103\SQLEXPRESS;uid=sa;pwd=123;

注意 “Connection String”也是可以改名的,修改後儲存,見:

 

因為記錄日誌至資料庫,使用到了Data Access Application Block,所以這一步是配置了一個Data Access執行個體做為資料記錄源。配置完了,可以將LocalSqlServer刪除掉,不刪除也不影響,反正後邊也用不到。

5.按所示,從右鍵菜單建立一個“Logging Application Block”

 

在建立的Logging Application Block節點下自動出現了Filters,Category Source,Special Source,Trace Listeners,Formatters這樣的幾個節點,如下:

 

這五個節點說明
Filters:設定過濾器,可設定那些日誌會被記錄下來,可以按日誌分類,優先順序,自訂日誌過濾條件。
Category Source:按分類設定該分類的訊息記錄到的目標位置,如你可以設定A分類記錄在資料庫,而B分類記錄在文字檔;
Special Source:根據日誌的事件類型,來指定日誌記錄的目標位置,如你可以將錯誤&警告資訊記錄在資料庫,其它的事件類型的日誌記錄在系統日誌;
Trace Listeners:Enterprise Library 3.0 提供了10種Trace Listeners,分別用於將日誌資訊記錄到不同目的地中,可以記錄至文字檔,事件記錄,Email,XML文檔,資料庫,Msmq訊息佇列,windows的事件記錄等;
Formatters:它用於指定日誌資訊的格式,有三種格式:Text,Custom,Binary;

6.在Trace Listeners上右鍵,new->Database Trace Listener,如所示設定:

 

7.選擇Special Sources->Logging Errors & Warnings->Formatted EventLog TraceListener,將ReferencedTraceListener更換成:Database Trace Listener,將Formatter指定為Text Formatter,如:

 

8.同時將Category Sources-> General-> Formatted EventLog TraceListener中的ReferencedTraceListener也換成:Database Trace Listener,將Formatter指定為Text Formatter,如:。

 

9.在你的web頁中添加如下代碼,測試日誌記錄功能:


using System.Collections.Generic;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

//

        LogEntry log = new LogEntry();
        //事件ID
        log.EventId = 2000;
        //日誌優先順序
        log.Priority = 2;
        log.Severity = System.Diagnostics.TraceEventType.Error;
        log.Title = "日誌標題";
        log.TimeStamp = DateTime.Now;
        log.Message = "Test LogEntry 1";
        

        //日誌類別
        ICollection<string> coll = new List<string>();
        coll.Add("General");
        log.Categories = coll;

        //添加額外資訊
        Dictionary<string, object> dic = new Dictionary<string, object>();
        dic.Add("name", "yaoyao");
        dic.Add("sex", "男");
        dic.Add("age", "28");

        log.ExtendedProperties = dic;
        //寫入日誌
        Logger.Write(log);

開啟資料庫查看,正常的話,應該可以看見一條新記錄。

晚上寫的,有點累了寫的象流水帳,沒什麼心得可言,對於初次接觸Logging的同行來說,希望能通過一個簡單的過程,讓自己的Logging跑起來,我的目標就達到了。其它的,各位看官有問題請回複,園子裡有好多篇關於Logging Application Block 1.0/2.0的文章,可以搜尋一下,另外2.0和3.0的Logging Block使用上並沒有太大區別,可以參考一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.