Simon
Simon

Reputation: 8349

Get exception method name

In C# (MVC 5 in particular), how can I get the name of a method where an exception occurs?

Here is my current code:

public async Task TestException()
{
    int result;
    int divider;
    divider = 0;
    try
    {
        result = 1 / divider;
    }
    catch (Exception ex)
    {
        string methodName = ex.TargetSite.Name;
        throw;
    }
}

I have also tried:

methodName = new System.Diagnostics.StackTrace(ex).GetFrame(0).GetMethod().Name;

The method name is being reported as "MoveNext" in both of the above code examples.

Thanks

Upvotes: 2

Views: 127

Answers (0)

Related Questions