Posts Tagged ‘health reporting’

Health Monitoring in ASP.NET

August 22nd, 2009

Unless your code is perfect, you’re bound to get runtime errors. Exceptions may not get caught, connections may not get closed off, and eventually, at some point, you’ll divide by 0. While you can’t prevent them, you can be aware of them using ASP.NET’s built in health monitoring provider. Health monitoring is configured in the web.config and can be set to use various types of logging, from writing to a SQL server instance or generating an email, for specific types of errors.

There are several pre-defined events, providers, and rules inside the root web.config file in the same directory as machine.config (not to be confused with the web.config in your application). In the healthMonitoring section you will see several groupings for providers, rules, profiles, and event mappings. For right now, we’ll focus on providers, rules, and eventMappings. I’ll discuss how these play into logging events.

Providers (<providers>) essentially tell where the log information is going. For example, the provider named EventLogProvider maps to the EventLogWebEventProvider class. As you’d expect, this provides all those error events you see in your Event Viewer. Similarly, the SqlWebEventProvider allows you to log events to the built in ASP.NET database (ASPNETDB.mdf in your app_data folder or your own SQL Server instance configured using aspnet_regsql.exe). Web can define a custom provider in your application web.config file (or in the root if you want to apply it to all your sites) to, for example, generate and send an email to a recipient containing logged event information. The SimpleMailWebEventProvider does just this. We will define this provider and use this provider in our example below.

Rules (<rules>) dictate which event type will use a provider. For example, you can have application errors write to the SQL provider or generate an email using your the email provider mentioned above. The eventName attribute, when adding a rule, specifies the name of the web event to use. Basically, for each rule, you specify the eventName and the provider. Simple as that.

Event Mappings (<eventMappings>) are used for mapping event names to provider classes. In the root web.config, you will notice several pre-defined provider names associated with event classes (the type attribute). If you are creating your own web event class (we’ll cover this in a later post), you may create a name for it and give it a type (your new class). By default, you can leave these alone, as the web events shipped with the framework cover most cases.

There are several event mappings listed, but there are usually one or two which, I feel, would be used in most conditions. First, the All Errors event mapping (using WebBaseErrorEvent) is used for all compilation, runtime, and configuration errors. I have found this to be the most handy. The second is the Failure Audits event (using the WebFailureAuditEvent class), which raises a an application page is accessed using improper credentials. By default, ASP.NET uses the NETWORK SERVICE account to access the file system, the registry, any databases, etc. If incorrect permissions are set in the file system, or malicious code is attempting to access the file system, this event will be raised. One other potentially useful web event is the Infrastructure Error mapping. This web event is raised only for compilation and configuration errors. However, I personally find it less useful, as the All Errors web event covers this.

In this post, I will create a basic health monitoring event handler using the SimpleMailWebEventProvider class to generate an email whenever any runtime error is found on the site. The email, by default, will provide all the information I need to diagnose the error and make corrections. You may customize this email template, but that is another topic.

First, since we’re using an email provider, we need to configure SMTP settings in the web.config. The SimpleMailWebEventProvider class will use these settings for sending the email. Configure your SMTP settings as follows:

<configuration>
    ...
    <system.net>
        <mailSettings>
            <smtp deliveryMethod="Network">
                <network host="SERVER" userName="USERNAME" password="PASSWORD" />
             </smtp>
        </mailSettings>
    </system.net>
    ...
</configuration>

Once that is entered, we can begin enabling the health monitoring features and chose our provider and rules. Again, the purpose is to create an email for all site errors and send them to my email address.

<system.web>
    ...
    <healthMonitoring enabled="true">
        <providers>
            <add name="EmailSiteErrors" type="System.Web.Management.SimpleMailWebEventProvider" to="ryan@marriedgeek.com" from="noreply@marriedgeek.com" buffer="false"/>
        </providers>
        <rules>
            <add name="Email Errors" eventName="All Errors" provider="EmailSiteErrors"/>
        </rules>
    </healthMonitoring>
    ...
</system.web>

That’s pretty much it. The ease of configuration and separation of providers and rules makes it a breeze to configure monitoring for your website. If you chose to use a SQL event provider, your logs will be written in the  provider database configured in your maching.config’s connection string. You may even have both or as many provider/rule sets as you like.

This is an overview of how to get started with using health monitoring in ASP.NET. To read more about health monitoring, check out the following links: