0

I am trying to get the method name and the class name where from exception is originated in my exception filter but unable to do so. I think because of the async methods used. I am getting MoveNext as the method instead of the actual method name.

Please find below the code

 public class ExceptionFilter : ExceptionFilterAttribute
    {
        /// <summary>
        /// Exception filter
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
        {
            
            var s = new StackTrace(context.Exception);
            var methodname=s.GetFrame(0).GetMethod().Name;


            Logger.LogError(context.Exception, context.Exception.Message);
            
            context.Response = context.Request.CreateResponse(HttpStatusCode.OK, res);
            return Task.CompletedTask;
        }

      }

I searched a lot but couln't find any answer. Any help would be appreciable.

Shahzad Ahamad
  • 809
  • 1
  • 11
  • 30

2 Answers2

0

You could create an extension like this and get the details in the case of async method.

public static class SampleExtension
{
    public static string GetOriginMethod(this MethodBase methodBase, [CallerMemberName] string memberName = "")
    {
        return memberName;
    }
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

I have found a way to get it.I am providing the answer so that it can help someone with similar situation.

I found the answer from this question

Please find the code below

 public class ExceptionFilter : ExceptionFilterAttribute
    {
        /// <summary>
        /// Exception filter
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
        {
            
            Logger.LogError(context.Exception, context.Exception.Message);

            var s = new StackTrace(context.Exception);
            var r = s.GetFrame(0);
            UtilityDAL.WriteExceptionLog("None", context.Exception, GetMethodName(r.GetMethod()), GetClassName(r.GetMethod()));

            context.Response = context.Request.CreateResponse(HttpStatusCode.OK, res);
            return Task.CompletedTask;
        }

        private string GetMethodName(System.Reflection.MethodBase method)
        {
            string _methodName = method.DeclaringType.FullName;

            if (_methodName.Contains(">") || _methodName.Contains("<"))
            {
                _methodName = _methodName.Split('<', '>')[1];
            }
            else
            {
                _methodName = method.Name;
            }

            return _methodName;
        }

        private string GetClassName(System.Reflection.MethodBase method)
        {
            string className = method.DeclaringType.FullName;

            if (className.Contains(">") || className.Contains("<"))
            {
                className = className.Split('+')[0];
            }
            return className;
        }
    }

It works fine for both async and non async functions.

Note: I am not sure if this will work fine for all the cases but till now it is working fine for me.

Shahzad Ahamad
  • 809
  • 1
  • 11
  • 30