Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Little Crash Reporter

3.23/5 (16 votes)
29 Apr 2009GPL32 min read 38.6K   462  
Collect crash data from a machine and send it to your email to be analyzed.

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.

C#
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.

Image 1

The end user can then either continue to execute the requested function or quit the application.

Image 2

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

Image 3

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

Image 4

How Little Crash Reporter Is Used

Before the crash reporter can work, the event handlers must be set to detect when an exception occurs.

C#
// Add event handler for thread exceptions
Application.ThreadException += 
  new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += 
  new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

And dont forget...

C#
// Event handler functions
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).

C#
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.

PHP
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";

To test it out, create a divide by zero exception.

C#
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

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)