ASP.NET Quick 'n Dirty Exception Logging*

If you need an easy way to handle logging exceptions in your ASP.NET web application but want to avoid all the complexities of using a logging library and / or setting up all the database "Stuff", here is  an easy way to handle it:

 

In web.config:

<appSettings>
    <add key="logExceptions" value="true"/>
  </appSettings > 
<system.web>

In Global.asax.cs:

public class Global : System.Web.HttpApplication
    {
        public static bool LogErrors = Convert.ToBoolean(ConfigurationManager.AppSettings["logExceptions"]);

//...

protected void Application_Error(object sender, EventArgs e)
{

    if (LogErrors)
    {
    Exception ex = Server.GetLastError().GetBaseException();
    File.AppendAllText(Server.MapPath("App_Data/exceptions.txt"),
               DateTime.Now.ToString() + ": \r\n" + ex.Message + "\r\n" + ex.StackTrace);
    Server.ClearError();
    }
}

 

What this does is grab the Exception (the one you didn't handle -- right? :-) and use the neato File.AppendAllText static method which will create the file if it isn't there, append your text, and close the file all in one swoop.    We write to our file in the /App_Data folder because that folder is automatically excluded from file change notification. We don't want our app recycling on us just because we logged an exception.

So by simply changing the appSettings element "logExceptions" to true, we can turn on "poor mans exception logging".

 

Silverlight Links

Thanks to an active developer / blogger community, my ittyurl.net site (which I originally developed for myself, but later realized that if I made it public, others might find it useful) -- now has a large number of active Silverlight links to their work, along with a few that I have added. Check it out if you are interested in Silverlight - I certainly am!

 

* Disclaimer
All increased intelligence due to reading this matter becomes the property of Peter Bromberg's UnBlog (hereafter referred to as "The UnBlog") and will not be acknowledged or returned. Neural damage is the responsibility of the reader. Attempts to enhance mental capacity as measured by standardized metrics neither legally nor ethically compel The UnBlog to make commensurate enhancements in quality or content. Readers with demonstrated cranial enlargement or cortical thickening may be required to sign a liability/publicity/copyright release and to appear in an article on the UnBlog, provided they are also the star of an upcoming motion picture.
The UnBlog reserves the right to cancel, terminate, or modify people who use brain-embiggening technology because look, dude, this is Peter Bromberg's UnBlog and we don't fool around. (acknowledgements to wired.com).

Comments

  1. Why not use ELMAH? It takes 2 minutes to setup. Drop the dll into your bin folder, add the module into the web.config and you are ready to go.

    ReplyDelete
  2. I am familiar with ELMAH and I have my own custom solution I wrote that I like better than ElMAH. But the point of the post was focused on "quick and dirty", not ELMAH -- or my custom solution either.

    ReplyDelete
  3. Anonymous4:47 PM

    LMAO! Nice disclaimer.

    ReplyDelete
  4. Hehe. I found it on some Wired article and without too much "adaptation", it seemed to fit.

    ReplyDelete

Post a Comment

Popular posts from this blog

Some observations on Script Callbacks, "AJAX", "ATLAS" "AHAB" and where it's all going.

IE7 - Vista: "Internet Explorer has stopped Working"

FIREFOX / IE Word-Wrap, Word-Break, TABLES FIX

System.Web.Caching.Cache, HttpRuntime.Cache, and IIS Recycles

FIX: Requested Registry Access is not allowed (Visual Studio 2008)