The Main() entrypoint in a WPF is auto-generated from the App.xaml source file. Earliest opportunity you have to subscribe the event is in the constructor for the App class in App.xaml.cs:
public partial class App : Application {
public App() {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// etc...
}
}
This is however not "perfect", you cannot detect any failure in field initializers of your Application class. As well as any JIT compilation failures of types you use in your Application class, missing assemblies or versioning problems being the usual troublemakers.
To avoid missing those, you need to give up on the auto-generated code and write your own Main() method. With the assumption that you did not heavily modify the app.xaml file. Delete that file from the project and add a new class, I'd suggest Program.cs :) And make it look similar to this:
using System;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication1 {
class Program {
[STAThread]
public static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Start();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Start() {
var app = new App();
app.Run(new MainWindow());
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// etc..
}
}
public class App : Application {
// etc..
}
}