norekhov
norekhov

Reputation: 4243

Does it makes sense to write minidump on Unhandled Exception in .NET?

It seems when I'm in exception handler like this:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

Or like this:

Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;

The stack was already unwind to call my custom unhandled exception handler. Seems it doesn't make sense to write a minidump at this point cause the stack was already unwind. Without unwinding the stack application can't understand whether this exception was unhandled or not.

Even if I can see stack in UnhandledExceptionEventArgs.ExceptionObject I can't get minidump at exact place where application crashed.

Is there another way?

I know I can ask system to write a dump but I should be Administrator for that.

UPDATE:

Ok. I've got an idea ) Would be nice if in FirstChanceException handler I can walk stack back and see if this exception is unhandled or not. But this should be fast enough to work in production.

Upvotes: 5

Views: 1496

Answers (2)

Sinatr
Sinatr

Reputation: 21999

I have never used dumps myself. It's usually enough to only know the place where exception occurs. But you are right, it would be much more handy to know values, etc. In the most recent version of error logger I even pass up to 3 optional parameters, which are dumped into a log, it helps. Something like (simplified):

public void Log(string tag, string message = "", Exception exception = null, [CallerMemberName] string caller = "",
    object param = null, object param2 = null, object param3 = null, object param4 = null)
{
    DateTime time = DateTime.Now;
    var method = caller;
    if (param != null || param2 != null || param3 != null)
        method = string.Format("{0}({1}{2}{3}{4})", caller, param != null ? param : "", param2 != null ? ", " + param2 : "",
            param3 != null ? ", " + param3 : "", param4 != null ? ", " + param4 : "");
    try
    {
        ...
        if (exception != null)
            using (StreamWriter file = new StreamWriter(_errorFileName, true))
            {
                file.WriteLine(string.Format("[{0}] {1} {2}: {3}", time, tag, method, message));
                file.WriteLine(exception);
            }
    }
    catch { }
}

And usage is

public static T Deserialize<T>(string file)
{
    try
    {
        ...
    }
    catch (Exception e)
    {
        Log("xml", exception: e, param: file, param2: typeof(T));
    }
    return default(T);

}

It is not an answer to your question, but a solution of how to live comfortably without dump.

Upvotes: 0

SLaks
SLaks

Reputation: 887453

You're looking for the FirstChanceException event, which is raised before the stack is unwound.

Upvotes: 2

Related Questions