0

I am trying to read method name from the exception which is thrown by a method but didn't find the solution.

Below is the exception handler.

public class ExceptionLogger
{

private readonly RequestDelegate next;

public UnhandledExceptionLogger(RequestDelegate next)
{
    this.next = next;
}

public async Task InvokeAsync(HttpContext httpContext)
{
    try
    {
        await this.next(httpContext).ConfigureAwait(false);
    }
    catch (Exception ex)
    {
        await this.HandleException(httpContext, ex).ConfigureAwait(false);
    }
}

private Task HandleException(HttpContext context, Exception exception)
{
    while (exception?.InnerException != null)
    {
        exception = exception?.InnerException;
    }

    // Need to send the method name where the exception has occurred.
    return context.Response.WriteAsync("");
}
}

Tried the soluptions provided at @Link1 and @ Link2

Balanjaneyulu K
  • 3,660
  • 5
  • 24
  • 45
  • You can check the call stack, but it is far more easier adding a couple of parameters to the `HandleException` method to give extra feedback `(HttpContext context, Exception exception, string method, string actionDoing)`. – Cleptus Jul 18 '19 at 10:18
  • 6
    this is what the `.StackTrace` is for; yes, it is a blob of text, but unrolling a stack reliably is tricky, and things like method names is only intended for developer usage anyway; IMO just dump the `.StackTrace`, but **not** to a user - just to your internal error logs. The end-user of a web-site has no business knowing anything about your method implementations. – Marc Gravell Jul 18 '19 at 10:21

0 Answers0