UnhandledException vs AppDomain

  • Thread starter Thread starter M. Jones
  • Start date Start date
M

M. Jones

Hallo,

I've got a small application that loads some "plug-ins" into individual
AppDomains. I would, for the "unlikely" case that one of these plug-ins
throw an unhandled exception log *all* information possible...

The handler is in place and works fine when it comes to simply catching the
unhandled exceptions.

But...

I notice that all unhandled exceptions thrown outside the default AppDomain
lose there stack traces - one of the things I really would like to log. A
unhandled exception thrown within the default AppDomain has all the stack
traces in place. I guess that this behavior makes sense somehow but It's
not really practical. Also the docs clearly say (and I have verified it)
that you can not add an unhandled exception handler to a child AppDomain.

Now the question is - how to I do it anyway - how to I log e.g. a Stack
Trace after an unhandled exception in an child AppDomain.

Any Ideas?

Thanks,
Michael
 
Hi Michael,

Thanks for your post. I am checking this issue and will update you with my
information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks!

But, and I hate to say this, it does not solve the problem (or am I
overlooking something). I can dump a stack trace fine (if the origin of the
Unhandled Exception is within the default AppDomain). It is catching an
Unhandled Exception within an child AppDomain where I fail....

I could use the try/catch if I would prohibit the plug-in of starting a new
thread - but that's not really an option as to now....

or - is there a possibility to intercept the creation of a new thread and
insert a "base" try/catch ??? (or is there generally the possibility to
intercept (rtl) calls from an e.g. Plug-In code??)

cu,
Michael
 
Hello Michael,

As I understand, the problem you are facing is unable to know how to get
the StackTrace of an Unhandled Exception thrown from child AppDomain.
Please correct me if there is any misunderstanding. I now share the
following information with you:

I built sample projects to test this issue: one is a class library which
throws an unhandled exception; the other is a console application which
creates and loads the class library in a new AppDomain and catch the
exception. Based on my test, it is the Exception.InnerException that
indicates the actual exception in the Child AppDomain. That is, we are able
to get the StackTrace of the unhandled exception thrown outside the default
AppDomain with the code like following:

e.InnerException.StackTrace

Please check it on your side. I look forward to your response.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hallo,

I have verified this and can not observe the same behaviour.

Try kicking of a thread within the Child AppDomain and then (after a few
moments) let this thread cause a exception. (this is just one of the tests
that I do)

Should I pack up the project and send it to you?

Regards and Thanks!

Michael
 
Oh - I forgot...

In my test app .InnerException is always null when the exception originates
from out of the Child Domain.

cu,
Michael
 
Hello Michael,

Thanks for your information. As stated in my previous email, I am not able
to reproduce the problem on my side. The following is detailed steps I was
doing:

1. Create a ControlLibrary project which throws an unhandled exception:
public Class1()
{
throw new Exception("1");
}

2. Create a Console application, which create a new AppDomain to load the
Class1 in the ClassLibrary and catch the exception:

//--------------------code snippet-----------------------
[STAThread]
public static void Main()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new
UnhandledExceptionEventHandler(MyHandler);

AppDomainSetup info = new AppDomainSetup();
info.ApplicationBase = "file:///" +
System.Environment.CurrentDirectory;

// Create an application domain with null evidence
AppDomain dom = AppDomain.CreateDomain("RemoteDomain", null, info);

BindingFlags flags = (BindingFlags.Public | BindingFlags.Instance |
BindingFlags.CreateInstance);

try
{
dom.CreateInstanceFromAndUnwrap("ClassLibrary1.dll",
"ClassLibrary1.Class1");
}
catch (Exception e)
{
Console.WriteLine("Catch clause caught : " + "Stack Trace = " +
e.InnerException.StackTrace);
}

dom.CreateInstanceFromAndUnwrap("ClassLibrary1.dll",
"ClassLibrary1.Class1");
}

static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("Catch clause caught : " + "Stack Trace = " +
e.InnerException.StackTrace);
}
//------------------end of-----------------------------

3. In both cases, the InnerException.StackTrace displays the message:
" at ClassLibrary1.Class1..ctor()"

Could you please check it on your side? In the meantime, I belive it's most
helpful if you can post a simple reproducible project.

I look forward to your response.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Michael,

Nice to hear from you. I am checking your sample project now. In the
meantime, could you please tell me whether or not my code demonstrates the
problem on your side?

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Well...

It works - while within the "sequence" of execution from the main AppDomain.
Yet if I start any thread, call anything via delegate or execute a operation
with a Async Handler then a exception will be caught but again with stack
trace...

Regards,
Michael
 
Hello Michael,

Thanks for your update. I found that the exception thrown from a thread
other than main thread in the child domain will cause the problem. It seems
to me to be a limitation. I am afraid that there is no good method to work
around this problem.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi,

not good news that is... But thanks for your effort non the less!

Is there maybe a way that I can "hook" into the creation of a new thread?


Regards,
Michael
 
Hi Michael,

Thanks a lot for your feedback.

You can use .NET Profiling API to get notified when a thread is created.
Please refer to the following article and sample:

The .NET Profiling API and the DNProfiler Tool
http://msdn.microsoft.com/msdnmag/issues/01/12/hood/

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for the link... I guess I'll have to spend some time digging through
it ...

Thanks and regards,

Michael
 
Back
Top