ASP.NET 2.0中的健康监测系统(Health Monitoring) - 基本应用
<configuration> <system.web> <healthMonitoring enabled="true"> <eventMappings> <clear /> <!--记录所有错误事件--> <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent" startEventCode="0" endEventCode="2147483647" /> <!--记录程序开始和结束事件--> <add name="Application Events" type="System.Web.Management.WebApplicationLifetimeEvent" startEventCode="0" endEventCode="2147483647"/> </eventMappings> <providers> <clear /> <add connectionStringName="SqlConnectionString" maxEventDetailsLength="1073741823" buffer="false" name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" /> </providers> <rules> <clear /> <add name="All Errors Default" eventName="All Errors" provider="SqlWebEventProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" custom="" /> <add name="Application Events Default" eventName="Application Events" provider="SqlWebEventProvider" profile="Default" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" custom="" /> </rules> </healthMonitoring> </system.web> </configuration>
像上面这样配置完后,无论程序的开始或是停止,又或是发生了错误事件,其事件信息都会被记录到App_Data文件夹下的ASPNETDB.MDF数据库。(如果这个数据库不存在,则它会被自动地创建。)
在本文的结尾处你可以下载到一个简单的web站点程序,它演示了这个功能。 该程序包括两个网页,Default.aspx和Log.aspx。Default.aspx内有一个按钮,当点击了这个按钮后就会触发ApplicationException。 Log.aspx中使用的是GridView,DetailsView和两个SqlDataSource控件,用于显示aspnet_WebEvent_Events表中的事件信息。

注意,每一个事件都有与其相关联的EventCode值。 参考Erik Reitan的BLOG文章 - FAQ - Health Monitoring in ASP.NET 2.0,它里面有一个表格列出了事件代码和事件类型(当然文中还有其它有用的信息)。 (译者注:我把它转载过来,如下表所示)
|
InvalidEventCode
|
-1
|
|
UndefinedEventCode
|
0
|
|
UndefinedEventDetailCode
|
0
|
|
ApplicationCodeBase
|
1000
|
|
ApplicationStart
|
1001
|
|
ApplicationShutdown
|
1002
|
|
ApplicationCompilationStart
|
1003
|
|
ApplicationCompilationEnd
|
1004
|
|
ApplicationHeartbeat
|
1005
|
|
RequestCodeBase
|
2000
|
|
RequestTransactionComplete
|
2001
|
|
RequestTransactionAbort
|
2002
|
|
ErrorCodeBase
|
3000
|
|
RuntimeErrorRequestAbort
|
3001
|
|
RuntimeErrorViewStateFailure
|
3002
|
|
RuntimeErrorValidationFailure
|
3003
|
|
RuntimeErrorPostTooLarge
|
3004
|
|
RuntimeErrorUnhandledException
|
3005
|
|
WebErrorParserError
|
3006
|
|
WebErrorCompilationError
|
3007
|
|
WebErrorConfigurationError
|
3008
|
|
WebErrorOtherError
|
3009
|
|
WebErrorPropertyDeserializationError
|
3010
|
|
WebErrorObjectStateFormatterDeserializationError
|
3011
|
|
AuditCodeBase
|
4000
|
|
AuditFormsAuthenticationSuccess
|
4001
|
|
AuditMembershipAuthenticationSuccess
|
4002
|
|
AuditUrlAuthorizationSuccess
|
4003
|
|
AuditFileAuthorizationSuccess
|
4004
|
|
AuditFormsAuthenticationFailure
|
4005
|
|
AuditMembershipAuthenticationFailure
|
4006
|
|
AuditUrlAuthorizationFailure
|
4007
|
|
AuditFileAuthorizationFailure
|
4008
|
|
AuditInvalidViewStateFailure
|
4009
|
|
AuditUnhandledSecurityException
|
4010
|
|
AuditUnhandledAccessException
|
4011
|
|
MiscCodeBase
|
6000
|
|
WebEventProviderInformation
|
6001
|
|
ApplicationDetailCodeBase
|
50000
|
|
ApplicationShutdownUnknown
|
50001
|
|
ApplicationShutdownHostingEnvironment
|
50002
|
|
ApplicationShutdownChangeInGlobalAsax
|
50003
|
|
ApplicationShutdownConfigurationChange
|
50004
|
|
ApplicationShutdownUnloadAppDomainCalled
|
50005
|
|
ApplicationShutdownChangeInSecurityPolicyFile
|
50006
|
|
ApplicationShutdownBinDirChangeOrDirectoryRename
|
50007
|
|
ApplicationShutdownBrowsersDirChangeOrDirectoryRename
|
50008
|
|
ApplicationShutdownCodeDirChangeOrDirectoryRename
|
50009
|
|
ApplicationShutdownResourcesDirChangeOrDirectoryRename
|
50010
|
|
ApplicationShutdownIdleTimeout
|
50011
|
|
ApplicationShutdownPhysicalApplicationPathChanged
|
50012
|
|
ApplicationShutdownHttpRuntimeClose
|
50013
|
|
ApplicationShutdownInitializationError
|
50014
|
|
ApplicationShutdownMaxRecompilationsReached
|
50015
|
|
StateServerConnectionError
|
50016
|
|
AuditDetailCodeBase
|
50200
|
|
InvalidTicketFailure
|
50201
|
|
ExpiredTicketFailure
|
50202
|
|
InvalidViewStateMac
|
50203
|
|
InvalidViewState
|
50204
|
|
WebEventDetailCodeBase
|
50300
|
|
SqlProviderEventsDropped
|
50301
|
|
WebExtendedBase
|
100000
|
结论
ASP.NET 2.0内置的健康监测系统可以非常容易地使指定的事件自动地记录到指定的日志源。 本文中,我们研究了两种内置日志源:“EventLogProvider”和“SqlWebEventProvider”,它们会分别记录事件信息到Windows的事件日志中和SQL Server数据库的一个表里。 健康监测系统可以捕获自定义事件,也可以使用其它日志源。 在后面的文章中,我们将会看到更多的日志源,甚至是创建我们自己的日志源。 我们也会知道如何创建自己的事件,以及如何自动地触发一个事件。
到这里本文结束。祝编程愉快!