Getting exceptions that caused program crashes while not in debug mode

I have a C # application that I want to run just by running .exe from my desktop. However, I am sure there will be some kind of error that will lead to the program crashing. Is there a way to write the problem that caused the program to crash into a text file so that I can understand what caused the problem when users use the program? I know that I can use debug mode for this, but I want to run the application as a standalone, not inside VS.

Thank,

I know try catch blocks, and I already use those where problems may occur. But I speak generally. For example, if I was not sure where the problem occurred. Cannot print this specific error in the file.

+3
source share
6 answers

You can try the global try / catch method, except that if there is an exception in the background thread, it will not be detected. You can use AppDomain.UnhandledExceptionit if you want to be notified of any unhandled exception in appdomain ( msdn ). You should register mainly before the rest of your program runs like this:

static void Main(string[] args)
{
  AppDomain.UnhandledException += WriteUnhandledExceptionToFile;

  // rest of program
}

static void WriteUnhandledExceptionToFile(object sender, UnhandledExceptionEventArgs args)
{
   // write to where ever you can get it.
   string path = Path.Combine(Environment.CurrentDirectory, "UnhandledException.txt");
   File.WriteAllText(path, args.ExceptionObject.ToString()); // will print message and full stack trace.
}

Edit

Note that by default, Windows Forms and WPF catch all exceptions that occur in the user interface thread. You will need to subscribe to the Application.ThreadException event (forms) or the Application.DispatcherUnhandledException event (wpf) in order to receive notification of exceptions from these threads. The code will be very similar to the code above for the AppDomain event.

+2

, .

Main try{}catch{}, catch.

try
{
  // Calls to application code
}
catch(Exception ex)
{
   // log `ex.ToString()`
   throw; // rethrow to ensure termination optionally: `Application.Exit`
}
0

catch catch

try
{
   ///Your code
}
catch(Exception exception)
{
   System.IO.File.WriteAllLines("ErrLog.txt", exception.Message);
}

ToLog , .

public static void ToLog(this Exception Exception)
{
        using (var file = File.AppendText("ErrorLog.txt"))
        {
           file.WriteLine(DateTime.Now + " : " + exception.Message);
        }
}

catch,

 catch(Exception exception)
 {
    exception.ToLog();
 }
0

, .

0

, , try/catch #. , .

, , . - . Exception, , . , logging , .

- - . , , , . , . , . , throw ( , ).

, , , , catch . , /, , .

0

See the source information here http://www.csharp-examples.net/catching-unhandled-exceptions/

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
  MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
}

The UnhandledException event handles fuzzy exceptions thrown from the main user interface thread. The ThreadException event handles thrown exceptions from threads other than the UI.

I would replace MessageBox with some actual logging (log4net or others). This will give you the opportunity to output errors to another server for distributed applications, the file system for local users, event logs, options quite unlimited if you want to set the time.

0
source

All Articles