views:

162

answers:

4

I want to get function name of the exception thrown from dll in asp.net.

A: 

You should be able to make use of the Stack Trace

Exception.StackTrace Property

StackTrace Class

Represents a stack trace, which is an ordered collection of one or more stack frames.

StackFrame Class

A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread. The stack frame always includes MethodBase information, and optionally includes file name, line number, and column number information.

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.

astander
A: 

You can create a StackTrace class from the exception and analyze the frames.

For example:

public void Throw()
{
    throw new MyException();
}

public void CallThrow()
{
    Throw();
}

[Test]
public void GetThrowingMethodName()
{
    try
    {
        CallThrow();
        Assert.Fail("Should have thrown");
    }
    catch (MyException e)
    {
        MethodBase deepestMethod = new StackTrace(e).GetFrame(0).GetMethod();
        string deepestMethodName = deepestMethod.Name;
        Assert.That(deepestMethodName, Is.EqualTo("Throw"));
    }
}
Elisha
Still I cant get the name of the method, tell me how to get name of the method from DLL that throw exception.
ram
@ram, what's the result you're getting? Can you post an example of you're code and the call to the external assembly that throws the exception?
Elisha
Here my example code:void test(){ /* Exception Comes here */}public class method{void load() { test(); }}Here class name'method' was a dll file,im getting the exception from the method 'test()'. But the stack trace returns the name of the method as 'load()'. I need to get the method name 'test()'.
ram
A: 

You can try stacktrace object with the help of system.diagnostic namespace here is the linqpad testing source code that you can try out.

void Main()
{
    try {
        test();
    }
    catch(Exception ex) {
        StackTrace st = new StackTrace();
        st.GetFrame(1).GetMethod().Name.Dump();
    }
}

// Define other methods and classes here


public void test()
{
    throw new NotImplementedException();
}
Tumbleweed
A: 

Checkout StackTrace.GetFrame Method

KMan