Looking for a contract position
in Toronto area as a Senior .NET Developer
Toronto, ON, M6P 2P2
Tel: 647.328.3809
Email: 

ASP.NET Health Monitor with ASP.NET 3.5

Health monitor is a system to monitor health of the application. It’s easy to add to your website and easy to use.

You can subscribe to some events occurred in the application, for example, “Error” and log it, or sent per email.

To add it you have to modify your web.config file, just add this lines of code in system.web section

<healthMonitoring enabled="true">
            <eventMappings>
                <clear/>
                <add 
                    name="All Errors" 
                    type="System.Web.Management.WebBaseErrorEvent, 
                    System.Web, Version=2.0.0.0, Culture=neutral, 
                    PublicKeyToken=b03f5f7f11d50a3a" 
                    startEventCode="0" 
                    endEventCode="2147483647"/>
            </eventMappings>
            <providers>
                <clear/>
                <add
                    name="mailWebEventProvider"
                    type="System.Web.Management.SimpleMailWebEventProvider"
                    from="name@test.com"
                    to="name@test.com"
                    buffer="false"
                    subjectPrefix="An error occurred: "/>
            </providers>
            <rules>
                <clear/>
                <add
                    name="Testing mail event provider"
                    eventName="All Errors"
                    provider="mailWebEventProvider"
                    profile="Default"
                    minInstances="1"
                    maxLimit="Infinite"
                    minInterval="00:01:00"
                    custom=""/>
            </rules>

        </healthMonitoring>

And add in your system.net section this code

<mailSettings>
            <smtp 
                deliveryMethod="Network" 
                from="name@test.com">
                <network 
                    host="mailhost" 
                    port="25" 
                    defaultCredentials="true"/>
            </smtp>
        </mailSettings>

The explanation what this code does is very simple, firstly, in eventMapping section you subscribes to some event occurred in the application. There are a great deal of event which you can subscribe, you can find info about they in MSDN documentation.

Than in providers section you need to define provider, it can be email, database or some other provider. In my example I am using email provider, so all messages will be sent per email.

And than in rules section you have to bind events to providers.

Because I am using email provider, I have to define mailSettings settings in system.net section.

And it’s all. Now after error messages you will get the text like this:

** Application Information **
---------------
Application domain: 8487b6cb-4-128819412246400827 Trust level: Full Application Virtual Path: /WebSite Application Path: C:\ProjectLocker\Trunk\LightCMS\WebSite\
Machine name: xxx

** Events **
---------------
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 3/19/2009 4:00:29 PM
Event time (UTC): 3/19/2009 1:00:29 PM
Event ID: f6146a7781d34b9698cc5741b2aa75bd Event sequence: 15 Event occurrence: 1 Event detail code: 0

Process information:
    Process ID: 2420
    Process name: WebDev.WebServer.EXE
    Account name: xxx\xxx

Exception information:
    Exception type: System.DivideByZeroException
    Exception message: Attempted to divide by zero.

Request information:
    Request URL: http://localhost:7323/WebSite/CssVariables.aspx
    Request path: /WebSite/CssVariables.aspx
    User host address: 127.0.0.1
    User:
    Is authenticated: False
    Authentication Type:
    Thread account name: xxx\xxx

Thread information:
    Thread ID: 4
    Thread account name: xxx\xxx
    Is impersonating: False
    Stack trace:    at CssVariables.Button1_Click(Object sender, EventArgs e) in c:\ProjectLocker\Trunk\LightCMS\WebSite\CssVariables.aspx.cs:line 18
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

 
Powered by AtomicCms content management system