Thursday, August 16, 2007

Health Monitoring .NET 2.0

Health Monitoring .NET 2.0:

Probably this is known information,but health monitoring in .NET 2.0 reduces a
whole lot of coding.If it were to be done in 1.1 it requires a lot of developer effort.

Ok,diving into what is healthMonitoring: It monitors a running Application and does logging like
Any errors, any failures,restarts, along with custom events too (involves some coding)

Actually just by configuring the web.config, you can log events in application event viewer
By configuring the Sql Server database aspnetdb, you can log events in the database without a
single line of code.


A simple example of healthMonitoring:

Use this code snippet below under tag in web.config file

create a simple object reference not set error in your code.

Build the application, run it to see the Server error page.

Now open ControlPanel-->AdministrativeTools -->EventViewer -->Application and look for the
most recent entry. you will see the log information of the error.


<healthmonitoring>
<providers>
<clear>
<add name="EventLogProvider" type="System.Web.Management.EventLogWebEventProvider">
</add>

<eventmappings>
<clear>

<!--Log all default error events-->
<add name="All Errors" type="System.Web.Management.WebBaseErrorEvent">

</add>
-->
<rules>
<clear>
<!--Logs all webbaserrors to windows event log-->
<add name="All Errors Default" eventname="All Errors" provider="EventLogProvider" mininstances="1" maxlimit="Infinite" mininterval="00:00:00">
</add>
</clear>
</healthmonitoring>


HealthMonitoring Explained in detail:


The health monitoring tag in web.config goes under the .

<system.web>.
<healthmonitoring>
......
......

</healthmonitoring>
</system.web>

There are five major subsections to health monitoring:

They are:

providers
profiles
bufferModes
eventMappings
rules

providers: They provide the functionality for an API. It is a contract between an API and
the business logic/Data Abstraction layer. [From MSDN].

healthMonitoring providers, there are several of inbuilt providers
The default providers available are SqlWebEventProvider,EventLogProviders,WmiWebEventProvider

<providers>
<clear>
<add name="SqlWebEventProvider" connectionstringname="YourConnectionString" maxeventdetailslength="1073741823"><!-- This the maximum length of the event details -->
buffer="true" <!-- Default is false, if you set to true, you have configure the buffer mode property-->
bufferMode="Analysis"
type="System.Web.Management.SqlWebEventProvider,System.Web,
Version=2.0.0.0,Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>

<add name="EventLogProvider" type="System.Web.Management.EventLogWebEventProvider">
</add>


bufferModes: This is used to configure the buffering property of a provider. This is configured
to reduce performance impact and overhead of recording events.

The different bufferModes are CriticalNotification,Notification,Logging and Analysis
You can also configure your own custom bufferMode

<buffermodes>
<add name="Analysis" maxbuffersize="1000" maxflushsize="100" urgentflushthreshold="100" regularflushinterval="00:05:00" urgentflushinterval="00:01:00" maxbufferthreads="1">
</add>


name property above is used by the tag to reference it in the bufferMode property
maxBufferSize: maximum number of events that can be buffered by a provider before flushing
them and writing them to store

In our example above, I have used 1000 as the maximum number of events

maxFlushSize: How many events to flush at one time. this value can be between 1 and maxBufferSize

In our example above, I have used 100 events to be flushed at one go!

urgentFlushThreshold: minimum number of events after which the flush should occur


regularFlushInterval: This is the time interval per flush. Its value cannot be zero.


Every five minutes the flush would happen and be written to database


urgentFlushInterval:minimum time between flushess. I have used 1 min

maxBufferThreads: This is the maximum number of threads used for flushing.


profiles: I have not used this property in healthMonitoring. From what I have read,
they are used to determine how each event is collected and raised to the providers.
we have 2 providers by default.
They are Critical and Default
You can also write your custom profile.


eventMappings:

they specify which events you want to monitor. For example you can mention "All Events" happening,
or all "All Errors" , or "Heartbeats" or any custom event you would like to raise.
eventMappings can be done like the below:


<eventmappings>
<clear>
<!--Log all default error events-->
<add name="All Errors" type="System.Web.Management.WebBaseErrorEvent">
startEventCode="0"
endEventCode="2147483647"/>
</add>


rules: These are the part of the healthMonitoring which specify which events have to be logged
through which provider.

so for example:

<rules>
<clear>

<!--Logs all webbaserrors to windows event log-->
<add name="All Errors Default" eventname="All Errors" provider="EventLogProvider" mininstances="1" maxlimit="Infinite" mininterval="00:00:00">
</add>


This one helps us to log all the errors occured, uses the reference to the event name "All Errors"
and uses the provider "EventLogProvider" to log any error in the application to the Event Log viewer


Next article :how to configure your sqlserver database to log events.


Here are few good links for further reading:

how to raise custome events using healthMonitoring: http://msdn2.microsoft.com/en-us/library/ms998306.aspx
FAQ's of healthMonitoring: http://blogs.msdn.com/erikreitan/archive/2006/05/22/603586.aspx





1 comment:

Anonymous said...

Hey - I am certainly delighted to find this. great job!