Log only the start and end of an outage condition

Log only the start and end of an outage condition

by JBrooks 22. April 2009 06:25

We have an asp.net page that auto refreshes every 8 seconds using   <meta http-equiv="refresh" content="8">

This page would pull data from a linked server and whenever that link or the target server were down it would fill up our error log with an entry every 8 seconds until it came back up.   This is not very useful, so we wanted an entry for when the exceptions started and then one more entry for when it cleared.   And I wanted a way of matching off each set of start / stop events. 

This is how I did it.  First I added the following property to my page:

private bool inErrorState

{ get { return Request.Cookies["myPageError"] != null; }

set

{

    if (value != inErrorState)

    {

        if (value)  // first error.

        {

            string errorGuid = Guid.NewGuid().ToString();

            cApp.db.LogException("myPageError start of error, ErrorGuid: " + errorGuid);

            Response.Cookies["myPageError"].Value = errorGuid;

            Response.Cookies["myPageError"].Expires = DateTime.Now.AddDays(100);

 

        }

        else // coming out of error.

        {

            if (Request.Cookies["myPageError"] != null)

            {

                cApp.db.LogException("myPageError end of error, ErrorGuid: " + Request.Cookies["myPageError"].Value);

                Response.Cookies.Set(Request.Cookies["myPageError"]);

                Response.Cookies["myPageError"].Expires = DateTime.Now.AddDays(-1);

            }

 

 

        }

    }

}

}

 

So when the error first occurs the errorGuid is created, logged and then saved in the cookie. When it comes out of the error condition the errorGuid is again logged, but with different text, and then the cookie is cleared.

To use this property I just needed to set it to false if the databinding was sucessful and to true in the catch block when an exception is thrown.  Like such:

  try

            {

                myGrid.DataBind();

                inErrorState = false;

             }

            catch (Exception ex)

            {

              …

              inErrorState = true;

             }

This is all it took to just capture the start of an error condition and the matching clear of that condition.

 

 

 

 

 

 

 

Tags:

ASP.Net