Just because I discovered this today (and I really like resurrecting old questions with not-too-helpful answers!), since .NET4, there is a new event on AppDomain
, which fires before any exception handling.
At some point in the setup code for your app (e.g. Program.Main
, Application_OnStart
, etc), you can add a handler:
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.FirstChanceException
+= FirstChanceException
// ...rest of your app startup
}
private static bool LogFirstChanceExceptions
{
get
{
return ConfigurationManager.AppSettings["logFirstChanceExceptions"]
.Equals(bool.TrueString)
}
}
private static void FirstChanceException(object sender,
FirstChanceExceptionEventArgs e)
{
if (e != null &&
e.Exception != null &&
LogFirstChanceExceptions)
{
Console.Error.WriteLine("First-chance {0}: {1}",
e.Exception.GetType(),
e.Exception.Message);
}
}
}
(Untested, no warranty, etc. Not pictured: any re-entrancy handling, in case the FirstChanceException
handler itself throws an exception)
Doesn't help the OP, but it might help people who can recompile, to include something that allows them to toggle this kind of tracing in the future.