|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionError handling is one of the more difficult tasks any developer or development team must implement. Teams face all sorts of challenges on how to handle exceptions within applications. Oftentimes, you end up "re-creating the wheel" for every application you design; since many believe that the application's provisions dictate the exception-handling methodology, you end up needing to recreate exception handling for each application you develop. Microsoft has provided you with a large method of exception management with their Exception Application Block, but for many application scenarios, this block may be too large. Though it provides many degrees of flexibility, it can be cumbersome to set up and configure, and in a lot of cases, even larger than the application you're developing! This article will present a far simpler approach, and provide you with a flexible library you can use to handle your own exceptions. The First things first - the IErrorReporter interfaceThe main component of the public interface IErrorReporter
{
bool ReportError(Exception exception);
}
Classes which implement this interface need only contain implementation for the single method The code download provided with this article contains a simple WinForms application which utilizes the public class CustomErrorTrapperExample : IErrorReporter
{
public bool ReportError(Exception exception)
{
try
{
string filename = @"C:\ExceptionLog.txt";
FileMode fm = FileMode.Append;
if(!File.Exists(filename))
{
fm = FileMode.Create;
}
StringBuilder sb = new StringBuilder();
FileStream fs = new FileStream(filename,fm);
sb.Append(DateTime.Now.ToString());
sb.Append(Environment.NewLine);
sb.Append("---------------------------------------------------------");
sb.Append(Environment.NewLine);
sb.Append(exception.ToString());
sb.Append(Environment.NewLine);
sb.Append("---------------------------------------------------------");
sb.Append(Environment.NewLine);
string s = sb.ToString();
Byte[] byt = Encoding.ASCII.GetBytes(s);
fs.Write(byt,0,byt.Length);
fs.Close();
return true;
}
catch
{
return false;
}
}
Though this is a relatively simple example, it demonstrates how you can, in your own application paradigm, write custom implementations that the So how can I make it flexible?The answer to this question is relatively simple. Using a custom configuration section in your app.config or web.config file, you'll provide some information that "drives" certain aspects of the <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ErrorTrapper"
type="System.Configuration.NameValueSectionHandler" />
</configSections>
<ErrorTrapper>
<add key="exceptionHandlerType"
value="ClientApplication.CustomErrorTrapperExample,
ClientApplication"/>
<add key="showUI" value="true"/>
<add key="uiLabel" value="Something Exploded!"/>
<add key="uiIntro"
value="We're sorry, but something has happened.
Would you like to report this explosion?"/>
</ErrorTrapper>
</configuration>
As you can see from this configuration section, we're going to use a relatively simple paradigm for configuring the
The first of these properties, Now - the WinForms interface!Microsoft Windows and some of the supporting applications provide us with an awesome feature - the ability to send errors as they occur to a Microsoft Web Service which tallies up the exceptions that happen in their applications. You've seen the dialog that pops up from time to time when certain programs "explode", providing you the option of reporting the error to Microsoft. Well, the
Now that you've got a reference, we'll continue the examination of the final three configuration elements. The first of these, a Boolean property called Next is the Finally, the Finally, Using the ErrorTrapperThe last piece of code we'll take a look at is from the sample download. This WinForms user interface provides two buttons that fire - or cause - simple exceptions. At the beginning of the application's execution, we'll simply handle the static void Main()
{
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(new Form1());
}
private static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
ErrorHandler.HandleError(e.Exception);
}
private void btnSimpleError_Click(object sender, System.EventArgs e)
{
throw new Exception("This is a simple error");
}
private void btnDbFailure_Click(object sender, System.EventArgs e)
{
SqlConnection cn = new SqlConnection("this will break");
cn.Open();
}
And that's about it! Here you have a simple, flexible, and totally customizable exception management tool, complete with a Windows XP-like user interface that can be displayed to your users should you want to provide them the option of reporting or not reporting exceptions as they occur! Happy coding! Note - this assembly has already been strong-named and documented for you. So feel free to drop it into your GAC and use it wherever you need!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||