23

I am new to C#. I am writing a small desktop form based application and I need to have this feature in the app.

If the application crashes at any time, there should be a last opportunity for the app to collect stack trace and send it back to me...

Please give me directions on this.

Do I need a try catch covering the main the entry point of my app ?? or what is the best way to handle such things in a C# app.

thank you,

1
  • 2
    "phone home" features of programs need to be designed carefully. Make sure you warn users and consider what data you'd better not to receive (passwords, names, be extremly careful if it can contain child information). Check this out too - Windows Error Reporting: Getting Started, at least privacy links. Commented Apr 18, 2012 at 5:08

4 Answers 4

35

To catch all unhandled exceptions, Add this to program.cs:

    [STAThread]
    static void Main()
    {
    AppDomain currentDomain = default(AppDomain);
    currentDomain = AppDomain.CurrentDomain;
    // Handler for unhandled exceptions.
    currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
    // Handler for exceptions in threads behind forms.
    System.Windows.Forms.Application.ThreadException += GlobalThreadExceptionHandler;
    ...
    }

private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
   Exception ex = default(Exception);
   ex = (Exception)e.ExceptionObject;
   ILog log = LogManager.GetLogger(typeof(Program));
   log.Error(ex.Message + "\n" + ex.StackTrace);
}

private static void GlobalThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
{
   Exception ex = default(Exception);
   ex = e.Exception;
   ILog log = LogManager.GetLogger(typeof(Program)); //Log4NET
   log.Error(ex.Message + "\n" + ex.StackTrace);
}

Stack trace you can get by exception.StackTrace

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the answer. I am comparing your answer with Lynn Crumbling's and I am wondering what is the benefit of this approach vs his ..?
@user1185422 Aseem's approach is a global solution and will catch even startup-time errors, .NET internal exceptions, asynchronous operations errors, etc. Lynn's approach is a local one and will catch only exceptions that occur inside methods called from MainEntryPoint synchronously. I wouldn't recommend Lynn's approach as a crash-report solution because you can miss whole classes of errors.
try catch blocks are good, but wrapping each and every line of code inside a try-catch can be tedious, plus there can be exceptions due to third party libraries, this approach solves these two issues as well. You can go ahead with the try-catch block approach, even then this is absolutely necessary.
@Pavel and Aseem -- thanks for the explanation. I'll be adopting this method in the future.
Thanks for the answer. I tried making exception in main thread or manually setup threads and it works, but in when I make exceptions in an ActionBlock method defined in System.Threading.Tasks.Dataflow the program does not throw the exception nor does it seem to trigger currentDomain.UnhandledException and the console program simply stuck.
|
4

If I were you, I would consider an all-in-one solution, such as the free and open source NBug framework,

http://nbug.codeplex.com/

1 Comment

Spent 30 minutes using its provided samples.. Even the sample configuration app crashed quite a bit.. The idea seems great though..
3

Take a look at Crypto Obfuscator which has a Automatic Exception Reporting feature.

Automatic exception reporting makes it extremely easy for you to catch any unhandled exceptions that occur in your software and for your users to easily report these exceptions to you with a single click of a button.

The exception reports include all pertinent information including full stack trace info along with the values of all method arguments and local variables, plus the system information, the time of the exception, the build number, and optional developer defined custom data like log files, screenshots, etc.

DISCLAIMER: I work for LogicNP Software, the developer of Crypto Obfuscator.

1 Comment

There are other similar commercial tools, such as Red-Gate's SmartAssembly, red-gate.com/products/dotnet-development/smartassembly :)
2

If you wrap your code in a try-catch, and an Exception occurs, you can get at the Exception to see the stack trace. Yes, you would add a try-catch to wrap your main entry point.

try
{
    MainEntryPoint();
}
catch (Exception exc)
{
   System.Diagnostics.Debug.Print(exc.Message);  // get at entire error message w/ stacktrace
   System.Diagnostics.Debug.Print(exc.StackTrace);  // or just the stacktrace
}

2 Comments

I'd love to know what benefits subscribing to UnhandledException has over using this approach.
Yes, I am trying to find a reason of using that method over this one as well. Is it perhaps catching exceptions for other non - UI threads in the app ? I am not sure if your solution catches exceptions for other threads as well ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.