Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / PHP

Little Crash Reporter

Rate me:
Please Sign up or sign in to vote.
3.23/5 (16 votes)
29 Apr 2009GPL32 min read 37.8K   457   53   8
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)


Written By
CEO Little Apps
Canada Canada

CEO and Founder of Little Apps. Our software programs include:





All of our software is licensed under the GNU General Public License 3.


Comments and Discussions

 
GeneralMy vote of 4 Pin
Farhan Ghumra17-Jun-12 23:12
professionalFarhan Ghumra17-Jun-12 23:12 
GeneralMy vote of 2 Pin
Mark Treadwell9-Aug-09 7:22
professionalMark Treadwell9-Aug-09 7:22 
General[My vote of 2] close but no cigar Pin
Donsw12-Jun-09 17:50
Donsw12-Jun-09 17:50 
GeneralI give you 3/5 Pin
Bishoy Demian11-May-09 4:51
Bishoy Demian11-May-09 4:51 
first, you should not create a report file undless user requested and this could lead to more exceptions if running in limited trust, and the user should choose whether to restart the application or continue executing his command or process.

and the article lacks any explaination of the code, and you should use ASP.net instead of PHP for the reporting page.

in total it is a good solution, but poor article and less compatibility in technologies.

"Imagination is more important than knowledge.."
{Albert Einstein}

GeneralMy vote of 2 Pin
Neville Franks30-Apr-09 1:35
Neville Franks30-Apr-09 1:35 
GeneralMy vote of 2 PinPopular
Michael B. Hansen29-Apr-09 20:35
Michael B. Hansen29-Apr-09 20:35 
GeneralI like it Pin
jpmik29-Apr-09 9:56
jpmik29-Apr-09 9:56 
GeneralMy vote of 1 Pin
Giga7721-Apr-09 3:48
Giga7721-Apr-09 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.