Introduction
Normally, when an exception occurs in .NET, the exception details are shown and the application either continues or fails. This source code allows you to collect the necessary information and send it to an SMTP server so that it will arrive in your mailbox.
Unhandled Exceptions
For example, take the following divide by zero exception that can occur in a .NET program.
int i = 0;
int j = 1 / i;
Once this exception is thrown, .NET will first see if there is anything that is trying to catch the exception, otherwise the following dialog will appear.
The end user can then either continue to execute the requested function or quit the application.
This is what it will look like if an exception occurs in a program with LCR built in
How Little Crash Reporter Works
The picture below shows how an error that occurs in your program is generated and sent to you
This is the class diagram of the program. The main functions used are the event handlers (Application_ThreadException
and CurrentDomain_UnhandledException
) that send a signal once there is an exception. After that, LCR takes it over from there...
How Little Crash Reporter Is Used
Before the crash reporter can work, the event handlers must be set to detect when an exception occurs.
Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
And dont forget...
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
CrashReporter ErrorDlg = new CrashReporter((Exception)e.ExceptionObject);
ErrorDlg.ShowDialog();
}
static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
CrashReporter ErrorDlg = new CrashReporter(e.Exception);
ErrorDlg.ShowDialog();
}
Even though the event args specifies just a normal exception object being sent, the actual exception is retrieved via typeof.
Next we need to configure the URL for the PHP script and the error directory (normally just leave it).
private readonly string BugReportURI = "http://www.bugreporturi.com/send.php";
private readonly string ErrorDirectory = string.Format(@"{0}\Errors",
Environment.CurrentDirectory);
Once that's finished, use the files in the PHP Uploader folder to configure the SMTP server, username and password for the emails. Then upload them to the web server you specified.
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
To test it out, create a divide by zero exception.
int i = 0;
int j = 1 / i;
Your program should now be ready to send bug reports.
Points of Interest
If you are looking for somewhere to upload files and send them to a secure SMTP server, just search Google for a free web host that has cPanel. Also, if you are editing the data being sent, please respect peoples privacy.
History
- Revision 1.0 - Article published.
- Revision 2.0 - Images added, explanation updated