How do I log ALL exceptions globally for a C# MVC4 WebAPI app?

后端 未结 5 1708
失恋的感觉
失恋的感觉 2020-11-30 16:31

Background

I am developing an API Service Layer for a client and I have been requested to catch and log all errors globally.

So, while something like an un

5条回答
  •  旧时难觅i
    2020-11-30 16:53

    Why rethrow etc? This works and it will make the service return status 500 etc

    public class LogExceptionFilter : ExceptionFilterAttribute
    {
        private static readonly ILog log = LogManager.GetLogger(typeof (LogExceptionFilter));
    
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            log.Error("Unhandeled Exception", actionExecutedContext.Exception);
            base.OnException(actionExecutedContext);
        }
    }
    

提交回复
热议问题