An introduction to ASP.Net health monitoring and web events

In this post I would to focus on ASP.Net Health monitoring and Web events.Health monitoring is another mechanism to capture/abstract logging.

In most cases with our ASP.Net applications we need to trace what happens when they are up and running. Have a look at a previous post of mine where I talk about Tracing at page and application level.

In this post I would like to talk about Web events.

The web event represents the data that is to be logged.We can raise those events and those events can be logged by various providers like email,SQL Server,EventLog.

Some of the most common events are WebAuditEvent that logs every time someone logs in successfully or unsuccessfully through forms authentication.

Another web event is WebBaseErrorEvent that logs any sort of unhandled exception.It is the base class for all the health-monitoring error events.

All these events are logged to the Event Log.Logging is turned on by default.

Have a look here to find more about ASP.Net health monitoring and web events.

I am going to demonstrate the various aspects of Health Monitoring with hands on demos.

1) Launch Visual Studio and create a new ASP.Net application. Choose C# as the development language.

2) Add a new page to your application. Name it Exception.aspx

3) Add a button on your web page and leave the default name. In the Click() event handling routine type

protected void Button1_Click(object sender, EventArgs e)
        {
          throw new ApplicationException("I am an exception!");
        }

4) When you run the page and click the button an exception is generated and you get back an ASP.Net error page with the exception details and the source error.

If you go and view the Event Log through the Event Viewer (Just type Event Viewer in the Run window in Windows)

Have a look at the picture below

 

 So we have lots of information we can look into regarding this exception. We can look at the stack trace, the user information, the exception information.

 5) To recap, exceptions and failed security audit messages sent to the EventLog provider.We can change that and log them to an SQL Server database. In order to do that we must create a database first and make changes to the web.config file.

I added this element in my web.config file.

  <healthMonitoring enabled="true" heartbeatInterval="10">
    <rules>
 
 <add name="TheRule" eventName="Heartbeats" provider="SqlWebEventProvider"/>
<add name="TheSecondRule" eventName="All Errors" 
provider="SqlWebEventProvider"/>

 
   </rules>
      </healthMonitoring>

 We have added a rule that maps a range of events to a provider.In our case to the SqlWebEventProvider.

 6) Now I need to setup the database.Ιn the web.config file I have to make some changes to the connectionStrings element to define the path and the

 <connectionStrings>
    <clear/>
  <add name="LocalSqlServer"
   connectionString="data source=.\SQLEXPRESS;database=aspnetdb;
   Integrated Security=SSPI" />
  </connectionStrings>

 Then obviously we need to generate this database.

Go to Start->All Programs-> Visual Studio->Visual Studio Command prompt and type aspnet_regsql. Then the wizzard pops up and by following the steps of the wizzard you will create the provider database.

Now if you go and refresh the databases list in the Object Explorer in SSMS you will see the new database created. Now I need to grant permissions to my web application to be able to write to it.My account name that I run the application under is  fofo-PC\fofo. This is the machine's administrator password and it is also an account (login) that belongs to the sysadmin fixed server role.So I do not need to do anything more in order to have write access to the database.

 7) View the Exception.aspx page in the browser once more and click the button to generate an exception. Obviously an exception will be genereated. Select the table (aspnet_WebEvent_Events) from your database, and do a (Select * from aspnet_WebEvent_Events). Have a look at the results. The HeartBeat events and the other errors are all in there.

The exception now will be both logged in the Event Log and the database.

Have a look at the picture below to see what I see when I get results back from the aspnet_WebEvent_Events table.

 

8)  Now we can add a rule that will map to email.In order to email we need to setup a provider and add a third rule so the we receive information (errors/exceptions) through the email.

Ηave a look at the changes in the 

      <healthMonitoring enabled="true" heartbeatInterval="10">
          <rules>
 
<add name="TheRule" eventName="Heartbeats" provider="SqlWebEventProvider"/>
<add name="TheSecondRule" eventName="All Errors" provider="SqlWebEventProvider"/>
<add name="TheEmailRule" eventName="All Errors" provider="EmailProvider"/>
        
     </rules>
          <providers>
              <add name="EmailProvider"
           type="System.Web.Management.SimpleMailWebEventProvider,
System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"
            from="admin@mysite.com"
            to="errors@mysite.com"
            buffer="false"
             />
              
              
          </providers>
      </healthMonitoring>

We also have to specify a smtp server. I am going to specify one in my web.config file.Obvioysly I am cheating a bit in this case.I am doing that so I can go on with my example.

In your case you will have to set up the smtp server with the actual settings provided to you by the system administrator.

  <system.net>
        <mailSettings>
 
      <smtp deliveryMethod="SpecifiedPickupDirectory" >
 
   <specifiedPickupDirectory pickupDirectoryLocation="c:\events\email" />
                
      </smtp>
            
        </mailSettings>
    </system.net>

9) Run the page again and click the button to generate an exception. Open the email item from the "C:\events\email" folder. Have a look at its contents. All the information is there.

Have a look at the picture below.

 

 

That is all folks!!!! Hope you found it interesting and learnt how to log events (Adding rules that map a range of events to a provider ) to the Event Log, email and the database.

Hope it helps!!!!

1 Comment

Comments have been disabled for this content.