当把数据库建完后(如果你使用的是App_Data文件夹内的ASPNETDB.MDF数据库,那么你就不需要做这件事了),你就可以在Web.config中添加<healthMonitoring>节点了。 通过下面示例那样的配置,就可以指示健康监测系统使用“SqlWebEventProvider”日志源记录所有的错误事件和所有的程序事件。 你应该在<providers>节点内重新设置“SqlWebEventProvider”。 我使用的是默认的连接字符串(“LocalSqlServer”),如果你不想使用ASPNETDB.MDF作为你的日志数据库,那么也可以修改这个名字,改为在Web.config的<connectionStrings>节点中注册的连接字符串的名字。
<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>
<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表中的事件信息。
在本文的结尾处你可以下载到一个简单的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,它里面有一个表格列出了事件代码和事件类型(当然文中还有其它有用的信息)。 (译者注:我把它转载过来,如下表所示)
结论
ASP.NET 2.0内置的健康监测系统可以非常容易地使指定的事件自动地记录到指定的日志源。 本文中,我们研究了两种内置日志源:“EventLogProvider”和“SqlWebEventProvider”,它们会分别记录事件信息到Windows的事件日志中和SQL Server数据库的一个表里。 健康监测系统可以捕获自定义事件,也可以使用其它日志源。 在后面的文章中,我们将会看到更多的日志源,甚至是创建我们自己的日志源。 我们也会知道如何创建自己的事件,以及如何自动地触发一个事件。
到这里本文结束。祝编程愉快!
InvalidEventCode
<?xml:namespace prefix = o />
|
-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数据库的一个表里。 健康监测系统可以捕获自定义事件,也可以使用其它日志源。 在后面的文章中,我们将会看到更多的日志源,甚至是创建我们自己的日志源。 我们也会知道如何创建自己的事件,以及如何自动地触发一个事件。
到这里本文结束。祝编程愉快!
转载于:https://blog.51cto.com/webabcd/345323