You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.4 KiB

  1. using Common.Models.RequestResponse;
  2. using Common.Utility;
  3. using log4net;
  4. using Newtonsoft.Json;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Web.Http.Filters;
  8. namespace ThirdPartyAPIs.CustomFilter
  9. {
  10. public class ApplicationExceptionLogFilter : ExceptionFilterAttribute
  11. {
  12. private readonly ILog _log = LogManager.GetLogger(typeof(ApplicationExceptionLogFilter));
  13. public override void OnException(HttpActionExecutedContext context)
  14. {
  15. var controllerName = context.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
  16. var actionName = context.ActionContext.ActionDescriptor.ActionName;
  17. string error = "Error Occured on api/" + controllerName + "/" + actionName + " And Error Is :" + context.Exception.Message;
  18. LogicalThreadContext.Properties["client_ip_address"] = new GetClientIpAddress().GetIpAddress(context.Request);
  19. LogicalThreadContext.Properties["exception"] = context.Exception;
  20. _log.Error(context.Exception.Message);
  21. TPResponse jsonResponse = new TPResponse();
  22. jsonResponse.ResponseCode = "999";
  23. jsonResponse.Msg = context.Exception.Message;
  24. jsonResponse.Data = context.Exception;
  25. var a = JsonConvert.SerializeObject(jsonResponse);
  26. context.Response = context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, a);
  27. }
  28. }
  29. }