Reputation: 107
We have a WPF windows desktop client application in which I have written logic for handling the Global unhandled exception and thread level exception and all.
But when I have tried with Null reference exception and its writing the logs correctly in my development machine using visual studio, But When we release this application and users installing in their machines because of some unhandled exception application is getting crashed and not writing the logs in users machines.
What I am missing here I dont know.
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown"
Startup="Application_Startup"
DispatcherUnhandledException ="AppDispatcherUnhandledException">
<Application.Resources>
</Application.Resources>
</Application>
private void Application_Startup(object sender, StartupEventArgs e)
{
// Global exception handling
try
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
System.Windows.Forms.Application.ThreadException += new
ThreadExceptionEventHandler(Application_ThreadException);
Application.Current.DispatcherUnhandledException += new
DispatcherUnhandledExceptionEventHandler(AppDispatcherUnhandledException);
}
catch (Exception exc)
{
Logger.LogEntry(LogModel.Create(this).Fatal("AppStart"));
Logger.LogEntry(LogModel.Create(this).Fatal(exc.StackTrace));
}
}
void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs args)
{
try
{
Exception exception = (Exception)args.ExceptionObject;
Logger.LogEntry(LogModel.Create(this).Fatal("DomainLevel"));
Logger.LogEntry(LogModel.Create(this).Fatal(exception.Message));
Logger.LogEntry(LogModel.Create(this).Fatal(exception.StackTrace));
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to the
event log. Reason: "
+ exc.Message);
}
finally
{
Logger.LogEntry(LogModel.Create(this).Fatal("Domain
Finally"));
Logger.LogEntry(LogModel.Create(this).Fatal(exc.StackTrace));
}
}
}
void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// Log the exception, display it, etc
try
{
Logger.LogEntry(LogModel.Create(this).Fatal("AppThread
Try"));
Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.Message));
Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.StackTrace));
}
catch (Exception exc)
{
Logger.LogEntry(LogModel.Create(this).Fatal("AppThread
Catch"));
Logger.LogEntry(LogModel.Create(this).Fatal(exc.Message));
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to the
event log. Reason: "
+ exc.Message);
}
}
void AppDispatcherUnhandledException(object sender,
DispatcherUnhandledExceptionEventArgs e)
{
{
// #if DEBUG // In debug mode do not custom-handle the
exception, let Visual Studio handle it
try
{
e.Handled = false;
// #else
ShowUnhandledException(e);
// #endif
}
catch (Exception exc)
{
Logger.LogEntry(LogModel.Create(this).Fatal(exc.StackTrace));
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to
the event log. Reason: "
+ exc.Message);
}
}
}
void ShowUnhandledException(DispatcherUnhandledExceptionEventArgs e)
{
try
{
Logger.LogEntry(LogModel.Create(this).Fatal("Dispatcher
try"));
Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.Message));
Logger.LogEntry(LogModel.Create(this).Fatal(e.Exception.StackTrace));
e.Handled = true;
}
catch (Exception exc)
{
Logger.LogEntry(LogModel.Create(this).Fatal("Dispatcher
Catch"));
Logger.LogEntry(LogModel.Create(this).Fatal(exc.Message));
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to the
event log. Reason: "
+ exc.Message);
}
}
public static void LogEntry(ILogEntry entry)
{
var log = LogManager.GetLogger(entry.SourceComponentName);
var message = string.Format("{0}::L{1} {2} - {3}", entry.SourceMemberName, entry.SourceLineNumber, entry.Text, entry.SourceFilePath.Split('\ \').LastOrDefault());
else if (entry.Level == LogLevelState.Error)
{
log.Error(message);
}
else if (entry.Level == LogLevelState.Fatal)
{
log.Fatal(message);
}
#if DEBUG
Console.WriteLine(message);
#endif
}
public static LogModel Create<T>(T source = default(T), [CallerMemberName] string callerMemberName = null, [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = 0)
{
return new LogModel
{
SourceComponentName = typeof(T).Name,
SourceMemberName = callerMemberName,
SourceFilePath = callerFilePath,
SourceLineNumber = callerLineNumber
};
}
public LogModel Fatal(string format, params object[] args)
{
Level = LogLevelState.Fatal;
Text = string.Format(format, args);
return this;
}
Edit:
In EventViewer
First Event:
Error 5/15/2018 11:32:52 PM .NET Runtime 1026 None
Application: MyApp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: exception code c0000008, exception address 00007FFECBB12B10
Second Event: Error 5/15/2018 11:32:54 PM Application Error 1000 Application Crashing Events
Faulting application name: MyApp.exe, version: 2.0.0.1769, time stamp: 0x5af68c0a Faulting module name: ntdll.dll, version: 10.0.16299.15, time stamp: 0x493793ea Exception code: 0xc0000008 Fault offset: 0x0000000000002b10 Faulting process id: 0x15bd4 Faulting application start time: 0x01d3eb91be5a688c Faulting application path: C:\Program Files\MyApp\MyApp.exe Faulting module path: C:\WINDOWS\SYSTEM32\ntdll.dll Report Id: 7c3cba0e-7438-491e-a9da-7871858212ef Faulting package full name: Faulting package-relative application ID:
Upvotes: 0
Views: 830
Reputation: 5421
Any of the code in your catch
blocks could potentially fail. This includes, but is not limited to:
LogEntry
method of your Logger
instanceLogEntryModel.Create
methodFatal
method of the LogEntryModel classIf your app is still crashing, and not logging anything, I would suspect the problem lies when calling the code inside the catch
blocks.
Upvotes: 1