简体   繁体   中英

Log unhandled exception

I have been trying to log this exception for days but cant figure it out. So I thought I post it here with the hope that someone could help. I have also posted some of my code that I'm currently using to catch unhandled exceptions but the exception never showed up in the log file.

I would appreciate if someone could help me.

Problem Event Name: APPCRASH
Application Name: Myapp.exe
Application Version: 1.0.0.0
Application Timestamp: 52e9aab8
Fault Module Name: clr.dll
Fault Module Version: 4.0.30319.18052
Fault Module Timestamp: 5173c26b
Exception Code: c00000fd
Exception Offset: 00372b52
OS Version: 6.1.7601.2.1.0.272.7
Locale ID: 1033
Additional Information 1: 5cff
Additional Information 2: 5cfff2c5825852a6f100872ec6f038a2
Additional Information 3: a2a3
Additional Information 4: a2a3734c473189c5efabc88b5081d05a

public MainWindow()
{
    Application.Current.DispatcherUnhandledException += CurrentOnDispatcherUnhandledException;
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    Dispatcher.UnhandledException += DispatcherOnUnhandledException;
    InitializeComponent();          
}

private void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.Exception;
    using (StreamWriter writer = File.AppendText("ExceptionsLog.txt"))
    {
        DateTime time = DateTime.Now;
        writer.WriteLine("Time {0} Exception: {1}", time, e);
        writer.Flush();
    }
    args.Handled = true;
    MessageBox.Show(e.ToString());
}

private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    using (StreamWriter writer = File.AppendText("ExceptionsLog.txt"))
    {
        DateTime time = DateTime.Now;
        writer.WriteLine("Time {0} Exception: {1} Runtime termination: {2}", time, e.Message, args.IsTerminating);
        writer.Flush();
    }
    MessageBox.Show(e.ToString());
}

private void CurrentOnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.Exception;
    using (StreamWriter writer = File.AppendText("ExceptionsLog.txt"))
    {
        DateTime time = DateTime.Now;
        writer.WriteLine("Time {0} Exception: {1}", time, e);
        writer.Flush();
    }
    args.Handled = true;
    MessageBox.Show(e.ToString());
}

For handling unhandled exceptions, please create a method like this

   protected TResult LogFailedOperations<TResult>(Func<TResult> action)
        {
            try
            {
                return action();
            }
            catch (Exception ex)
            {
                // Logic for logging from ex.Message
            }
        }

Also, when you perform any method, just enclose that method around this LogFailedOperations method like these examples -

private void MyButton_Click(object sender, RoutedEventArgs e)
{
  var result = LogFailedOperations(() => { /* Your logic for button click */ });
}

private void LoadDataToView()
{
  var result = LogFailedOperations(() => { /* Your logic for laoding data */ });
}

private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
  var result = LogFailedOperations(() => { /* Your logic for slider changed event */ });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM