Exception information logging

This commit is contained in:
Anton Kasyanov
2017-06-17 13:29:09 +03:00
parent 32790c6f26
commit 1fe2023b51
3 changed files with 54 additions and 3 deletions

View File

@@ -0,0 +1,50 @@
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace EveOPreview
{
// A really very primitive exception handler stuff here
// No IoC, no fancy DI containers - just a plain exception stacktrace dump
// If this code is called then something was gone really bad
// so even the DI infrastructure might be dead already.
// So this dumb and non elegant approach is used
sealed class ExceptionHandler
{
private const string ExceptionDumpFileName = "EVE-O Preview.log";
private const string ExceptionMessage = "EVE-O Preview has encountered a problem and needs to close. Additional information has been saved in the crash log file.";
public void SetupExceptionHandlers()
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += delegate (Object sender, ThreadExceptionEventArgs e)
{
this.ExceptionEventHandler(e.Exception);
};
AppDomain.CurrentDomain.UnhandledException += delegate (Object sender, UnhandledExceptionEventArgs e)
{
this.ExceptionEventHandler(e.ExceptionObject as Exception);
};
}
private void ExceptionEventHandler(Exception exception)
{
try
{
String exceptionMessage = exception.ToString();
File.WriteAllText(ExceptionHandler.ExceptionDumpFileName, exceptionMessage);
MessageBox.Show(ExceptionHandler.ExceptionMessage, @"EVE-O Preview", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch
{
// We are in unstable state now so even this operation might fail
// Still we actually don't care anymore - anyway the application has been cashed
}
System.Environment.Exit(1);
}
}
}