程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> Enterprise Library 2.0 技巧(4):如何用編程的方法來配置Logging Applicat

Enterprise Library 2.0 技巧(4):如何用編程的方法來配置Logging Applicat

編輯:關於ASP.NET

Enterprise Library 2.0 技巧(4):如何用編程的方法來配置Logging Application Block

在本系列的技巧(1)和技巧(2)中分別介紹了使用外部配置文件,使用數據庫記錄配置信息兩種方法,不知道大家有沒有想過不使用任何配置文件,也不使用數據庫而直接用編程的方法來實現呢?本文將會展示如何使用編程的方法來配置Logging Application Block。首先我們需要了解一下Logging Application Block中比較重要的幾個對象:

1.LogFormatter

格式化對象,LogFormatter有TextFormatter和BinaryFormatter兩種,多數情況下我們會使用TextFormatter,它通過一個Template來創建,一個完整的Template格式如下:

Timestamp: {timestamp}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {machine}{newline}
Application Domain: {appDomain}{newline}
Process Id: {processId}{newline}
Process Name: {processName}{newline}
Win32 Thread Id: {win32ThreadId}{newline}
Thread Name: {threadName}{newline}
Extended Properties: {dictionary({key} - {value})}{newline}

這裡就不具體解釋每一項的含義,大家可以參考有關文檔,示例代碼:

const string Template = "Timestamp: {timestamp}{newline}" +
"Message: {message}{newline}" +
"Category: {category}{newline}" +
"Machine: {machine}{newline}";
TextFormatter formatter = new TextFormatter(Template);

2.TraceListener

TraceListener提供了日志記錄服務,它指定的是日志將被記錄到何處,數據庫中或者是文本文件,Enterprise Library提供了7種

TraceListener:Database TraceListener、Email TraceListener、Flat File TraceListener、Formatter Event Log TraceListener、Msmq TraceListener、System Diagnostics TraceListener、WMI Trace Listener。每一種TraceListener都需要一個LogFormatter來對記錄的信息進行格式化,例如創建一個FlatFileTraceListener實例:

const string LogFilePath = @"d:\\share\\messages.log";
FlatFileTraceListener logFileListener =
new FlatFileTraceListener(LogFilePath,"----------","----------", formatter);

這裡的formatter就是在上面創建的TextFormatter對象。

3.LogSource

LogSource其實就是TraceListener的集合,Enterprise Library允許針對不同的日志信息記錄到不同地方,因此可以在LogSource中加入多個TraceListener:

LogSource mainLogSource =

new LogSource("MainLogSource", SourceLevels.All);

mainLogSource.Listeners.Add(logFileListener);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved