ASP.NET 2.0 Health Monitoring - Log Errors and Unhandled Exceptions to SQL Server
by David Hayden ( Microsoft MVP C# ), Filed: ASP.NET 2.0
Many times in your ASP.NET applications you just want to capture errors and unhandled exceptions in the eventlog or a database so you can monitor and eradicate errors and problems in your code that slipped through QA. You could use various logging solutions, like log4net or logging application block, and various tracelisteners to capture the errors in the global exception handler, or you could use ASP.NET 2.0 Health Monitoring which is built into System.Web.Management.
I like to log messages to SQL Server where appropriate so I can get at them easiest. ASP.NET 2.0 Health Monitoring contains several providers:
- SimpleMailWebEventProvider. This provider sends e-mail for event notifications.
- TemplatedMailWebEventProvider. This provider uses templates to define and format e-mail messages sent for event notifications.
- SqlWebEventProvider. This provider logs event details to a SQL Server database. If you use this provider, you should encrypt the connection string in your Web.config file by using the Aspnet_regiis.exe tool.
- EventLogWebEventProvider. This provider logs events to the Windows application event log.
- TraceWebEventProvider. This provider logs events as ASP.NET trace messages.
- WmiWebEventProvider. This provider maps ASP.NET health monitoring events to Windows Management Instrumentation (WMI) events.
SqlWebEventProvider will log events to a database. To use SqlWebEventProvider, a table and stored procedure need to be added to the database. One can add the necessary items using aspnet_regsql.exe:
- Trusted Connection: aspnet_regsql.exe -E -S serverName -d database -A w
- SQL Server Authentication: aspnet_regsql.exe -U userID -P password -S serverName -d database -A w
This will add a stored procedure, called aspnet_WebEvent_LogEvent, and a table, called aspnet_WebEvent_LogEvent:

Now in my web.config of my ASP.NET Application I need to enable Health Monitoring and specify what events I want to log as well as what provider, SqlWebEventProvider, I want to use to log the events:
<healthMonitoring enabled="true">
<eventMappings>
<clear />
<add
name="All Errors"
type="System.Web.Management.WebBaseErrorEvent"
startEventCode="0"
endEventCode="2147483647" />
</eventMappings>
<providers>
<clear />
<add
name="SqlWebEventProvider"
type="System.Web.Management.SqlWebEventProvider"
connectionStringName="ConnectionString"
maxEventDetailsLength="1073741823"
buffer="false" />
</providers>
<rules>
<clear />
<add
name="All Errors Default"
eventName="All Errors"
provider="SqlWebEventProvider"
profile="Default"
minInstances="1"
maxLimit="Infinite"
minInterval="00:00:00" />
</rules>
</healthMonitoring>
In this case I want to capture all errors and have them logged to the sql server database as specified in connectionStringName=“ConnectionString“, which is the same location where I ran the aspnet_regsql.exe to create the necessary table and stored procedure.
The beauty here is that if I later change to have the errors logged to the EventLog in addition to or in replace of the SQL Server Database, I can do that with the EventLogWebEventProvider via configuration without having to change any source code:
<healthMonitoring enabled="true">
<eventMappings>
<clear />
<add
name="All Errors"
type="System.Web.Management.WebBaseErrorEvent"
startEventCode="0"
endEventCode="2147483647" />
</eventMappings>
<providers>
<clear />
<add
name="EventLogProvider"
type="System.Web.Management.EventLogWebEventProvider" />
</providers>
<rules>
<clear />
<add
name="All Errors Default"
eventName="All Errors"
provider="EventLogProvider"
profile="Default"
minInstances="1"
maxLimit="Infinite"
minInterval="00:00:00" />
</rules>
</healthMonitoring>
I recommending looking at all the possible settings. You can add buffering and be more specific about which events to capture.
Good stuff!
by David Hayden ( Microsoft MVP C# ), Filed: ASP.NET 2.0