- Override
OnStartup
and OnExit
.
- Create TempDirectory (if needed)
- Implement
InstanceChecker
to prevent multiple instances of an application from running.
- Setup
CrashReporter
events and handlers
public static TempDirectoryHelper TempDirectory = new TempDirectoryHelper("APPLICATION_NAME");
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (!Debugger.IsAttached)
{
if (InstanceChecker.IsAppRunning(Process.GetCurrentProcess(), true))
Current.Shutdown(0);
// Attach Crash Reporter events
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
Current.DispatcherUnhandledException += DispatcherOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
System.Windows.Forms.Application.ThreadException += Application_ThreadException;
}
// Prism Bootstrapper
var bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
protected override void OnExit(ExitEventArgs e)
{
InstanceChecker.Cleanup();
base.OnExit(e);
}
#region Crash Reporter
private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
ReportCrash(e.Exception);
//Environment.Exit(0);
}
private void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
ReportCrash(e.Exception);
//Environment.Exit(0);
}
private void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
ReportCrash(e.Exception);
//Environment.Exit(0);
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
ReportCrash((Exception)e.ExceptionObject);
//Environment.Exit(0);
}
private static void ReportCrash(Exception exception, string developerMessage = "")
{
var crashReport = new CrashReport
{
FromEmail = "FROM",
ToEmail = "TO",
UserName = "USERNAME",
Password = "PASSWORD",
SmtpHost = "SMTPHOST",
Port = PORT,
EnableSSL = true,
IncludeScreenshot = true,
CaptureType = CaptureType.Screen,
DeveloperMessage = developerMessage,
Exception = exception,
};
crashReport.Send(exception);
}
#endregion
- Edit
.csproj
file.
<Target Name="BeforePublish">
<Error Condition="'$(DebugSymbols)' == 'true'" Text="Hey dumbass, dont publish using debug configurations!!" />
</Target>