I sometimes can't get the method name from my method in Silverlight

554 views Asked by At

I have this method that logs errors from the Silverlight client:

[MethodImpl(MethodImplOptions.NoInlining)]
public static bool ErrorHandler(Exception error, bool showError = true)
{
    string callingMethod = new StackFrame(1).GetMethod().Name;

    //write log - call webservice
    //show error that some error happened if showError is true
}

my problem is this, that when I run the app and call a method that throws an exception it logs like this:

<ThrowExceptionMethod>b__72: test

but when someone that has a normal (non-developer runtime) runs this app and throws an exception it logs like this:

b__72: test

I'm only guessing that this is due SDK Silverlight runtime. It's hard to tell, because most of my colleagues have Silverlight SDK installed... Could someone confirm if this is the reason for this anomaly.

SOLVED!

It was not due SDK runtime, it was just because I call anonymous methods. I added this line of code to it:

var listStackFrame = new List<StackFrame>();

for (int i = 1; i < 20; i++)
{
    listStackFrame.Add(new StackFrame(i));
}

string callingMethod = string.Empty; //new StackFrame(1).GetMethod().Name;

foreach (var item in listStackFrame
    .Where(m => m != null && m.GetMethod() != null && !string.IsNullOrEmpty(m.GetMethod().Name) && m.GetMethod().Name.Contains("__")))
{
    callingMethod += item.GetMethod().Name + "->";
}

I am not worried about how much this eats up resources, because after all an exception has occured and the user will stop whatever he is doing and contact support.

2

There are 2 answers

2
Ivo On BEST ANSWER

That's usually because what you are executing is actually a yield iterator or anonymous delegates delegate (there may be other cases I'm missing). What happens is that the compiler actually create new methods and even classes for most of the syntactic sugar.

I think you can iterate all the previous methods call looking for the first that doesn't have the [CompilerGenerated] attribute.

1
Ventsyslav Raikov On

This makes sense for the difference between a dev and normal machine:

StackFrame information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame objects.

That is from the StackFrame's documentation.