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:

    <add key="logExceptions" value="true"/>
  </appSettings > 

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();
               DateTime.Now.ToString() + ": \r\n" + ex.Message + "\r\n" + ex.StackTrace);


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).