Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to collect/report unexpected errors in .NET Window Applications?

I am looking for a better solution than what we currently have to deal with unexpected production errors, without reinventing the wheel.

A larger number of our products are WinForm and WPF applications that are installed at remote sites. Inevitably unexpected errors occur, from NullReferenceExceptions to 'General network errors'. Thus ranging from programmer errors to environment problems.

Currently all these unhandled exceptions are logged using log4net and then emailed back to us for analysis. However we found that sometimes these error 'reports' contain too little information to identify the problem.

In these reports we need information such as:

  1. Application name
  2. Application Version
  3. Workstation
  4. Maybe a screen shot
  5. Exception details
  6. Operating system
  7. Available RAM
  8. Running processes
  9. And so on...

I don't really want to re-invent the wheel by developing this from scratch. Components that are required:

  1. Error collection (details as mentioned above)
  2. Error 'sender' (Queuing required if DB or Internet is unavailable)
  3. Error database
  4. Analysis and reporting of these errors. E.g. 10 most frequent errors or timeouts occur between 4:00PM and 5:00PM. How do the errors compare between version x and y?

Note: We looked at SmartAssembly as a possible solution but although close it didn't quite met our needs and I was hoping to hear what other developers do and if some alternatives exist.

Edit: Thanks for the answers so far. Maybe I wasn't clear in my original question, the problem is not how to catch all unhanded exceptions but rather how to deal with them and to create a reporting engine (analysis) around them.

like image 323
Philip Fourie Avatar asked Sep 21 '08 07:09

Philip Fourie


People also ask

Which is the best way to handle errors in net?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.

Which of the following is the easiest way to track all unhandled error?

If your application has unhandled exceptions, that may be logged in the Windows Event Viewer under the category of “Application.” This can be helpful if you can't figure out why your application suddenly crashes. Windows Event Viewer may log two different entries for the same exception.

How do you handle exceptions in C#?

Exception Handling Using try-catch block When that statement is executed an exception is generated, which is caught by the catch block. The object of the type IndexOutOfRangeException is used to display a message to the user about the exception that has occurred.

What is application error C#?

Application level error handling allow to detect or handle web application level error handling so that its helps to handle and solve the particular error. Application level error handling can be done in 2 ways: 1. Add an Application_Error event handler to the global asax file for your web application.


2 Answers

I'd suggest Jeff Atwood's article on User Friendly Exception Handling, which does most of what you ask already (Application Info, Screenshot, Exception Details, OS, Logging to text files and Emailing), and contains the source code so you add the extra stuff you need.

like image 58
Andrew Avatar answered Oct 17 '22 20:10

Andrew


You can attach to the unhandled exception event and log it/hit a webservice/etc.

[STAThread]
static void Main() 
{
    Application.ThreadException += new ThreadExceptionEventHandler(OnUnhandledException);
    Application.Run(new FormStartUp());
}
static void OnUnhandledException(object sender, ThreadExceptionEventArgs t) 
{
    // Log
}

I also found this code snippet using AppDomain instead of ThreadException:

static class EntryPoint {
    [MTAThread]
    static void Main() {
        // Add Global Exception Handler
        AppDomain.CurrentDomain.UnhandledException += 
            new UnhandledExceptionEventHandler(OnUnhandledException);

        Application.Run(new Form1());
    }

    // In CF case only, ALL unhandled exceptions come here
    private static void OnUnhandledException(Object sender, 
        UnhandledExceptionEventArgs e) {
        Exception ex = e.ExceptionObject as Exception;
        if (ex != null) {
            // Can't imagine e.IsTerminating ever being false
            // or e.ExceptionObject not being an Exception
            SomeClass.SomeStaticHandlingMethod(ex, e.IsTerminating);
        }
    }
}

Here is some documentation on it: AppDomain Unhandled Exception

Outside of just handling it yourself, there isn't really a generic way to do this that is reusable, it really needs to be integrated with the interface of the application properly, but you could setup a webservice that takes application name, exception, and all that good stuff and have a centralized point for all your apps.

like image 23
sontek Avatar answered Oct 17 '22 19:10

sontek