http://davidhayden.com/blog/dave/archive/2006/02/15/2802.aspx
参考这篇文章配置Logging Application Block 的web.config如下:
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
configuration
>
<
configSections
>
<
section name
=
"
loggingConfiguration
"
type
=
"
Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
/>
<
section name
=
"
dataConfiguration
"
type
=
"
Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
/>
</
configSections
>
<
loggingConfiguration name
=
"
Logging Application Block
"
tracingEnabled
=
"
true
"
defaultCategory
=
"
General
"
logWarningsWhenNoCategoriesMatch
=
"
true
"
>
<
listeners
>
<
add source
=
"
Enterprise Library Logging
"
formatter
=
"
Text Formatter
"
log
=
"
Application
"
machineName
=
""
listenerDataType
=
"
Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
traceOutputOptions
=
"
None
"
type
=
"
Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
name
=
"
Formatted EventLog TraceListener
"
/>
<
add fileName
=
"
bin\trace.log
"
header
=
"
----------------------------------------
"
footer
=
"
----------------------------------------
"
formatter
=
"
PFPMIS Text Formatter
"
listenerDataType
=
"
Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
traceOutputOptions
=
"
None
"
type
=
"
Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
name
=
"
PFPMIS FlatFile TraceListener
"
/>
</
listeners
>
<
formatters
>
<
add template
=
"
Timestamp: {timestamp} Message: {message} Category: {category} Priority: {priority} EventId: {eventid} Severity: {severity} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties: {dictionary({key} - {value} )}
"
type
=
"
Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
name
=
"
PFPMIS Text Formatter
"
/>
<
add template
=
"
Timestamp: {timestamp} Message: {message} Category: {category} Priority: {priority} EventId: {eventid} Severity: {severity} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties: {dictionary({key} - {value} )}
"
type
=
"
Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
"
name
=
"
Text Formatter
"
/>
</
formatters
>
<
categorySources
>
<
add switchValue
=
"
All
"
name
=
"
Category
"
/>
<
add switchValue
=
"
All
"
name
=
"
General
"
>
<
listeners
>
<
add name
=
"
PFPMIS FlatFile TraceListener
"
/>
</
listeners
>
</
add
>
</
categorySources
>
<
specialSources
>
<
allEvents switchValue
=
"
All
"
name
=
"
All Events
"
/>
<
notProcessed switchValue
=
"
All
"
name
=
"
Unprocessed Category
"
/>
<
errors switchValue
=
"
All
"
name
=
"
Logging Errors & Warnings
"
>
<
listeners
>
<
add name
=
"
Formatted EventLog TraceListener
"
/>
</
listeners
>
</
errors
>
</
specialSources
>
</
loggingConfiguration
>
但是执行int error = Int32.Parse("bbbbb");
这句代码后trace.log 文件总为空,跟踪了一下,报下面这个异常:
“Microsoft.Practices.EnterpriseLibrary.Logging.Logger”的类型初始值设定项引发异常。
发现少了这几个dll,Microsoft.Practices.EnterpriseLibrary.Common.dll,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll,
Microsoft.Practices.ObjectBuilder.dll
赶快找来引用上,当然
Microsoft.Practices.EnterpriseLibrary.Logging.dll 这个dll是必不可少的。
执行一下,OK,打开trace.log 看到如下记录结果:
----------------------------------------
Timestamp: 2007-2-5 2:33:43
Message: 输入字符串的格式不正确。
SOURCE: mscorlib
FORM:
QUERYSTRING:
TARGETSITE: Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)
STACKTRACE: 在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
在 System.Int32.Parse(String s)
在 PFPMIS.Web.Index.WebForm1.Page_Load(Object sender, EventArgs e) 位置 H:\NewMis\Index\WebForm1.aspx.cs:行号 18
在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
在 System.Web.UI.Control.OnLoad(EventArgs e)
在 System.Web.UI.Control.LoadRecursive()
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Category: General
Priority: -1
EventId: 1
Severity: Information
Title:
Machine: XXZX-FNQF3DEF5X
Application Domain: b830af7a-3-128151164023437500
Process Id: 5320
Process Name: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe
Win32 Thread Id: 4556
Thread Name:
Extended Properties:
----------------------------------------
需要注意的是,如果忘记给track.log文件赋予asp.net用户写权限的话,将不会记录日志,而且不会报任何异常,在windows 2003 server下是给network service用户赋予写权限.
希望对使用Enterprise Library 2.0 Logging Application Block日志组件记录日志的朋友有所帮助