public void OnException(ExceptionContext filterContext) { var xForwardedFor = filterContext.HttpContext.Request.Headers["X-Forwarded-For"]; var userHostAddress = filterContext.HttpContext.Request.UserHostAddress; var hostAddresses = string.IsNullOrEmpty(xForwardedFor) ? userHostAddress : $"{xForwardedFor},{userHostAddress}"; var allIpAddresses = _multipleIpAddressProvider.GetIpAddresses(hostAddresses); var loggingModel = new MvcLoggingModel() { UserName = filterContext.HttpContext.User.Identity.Name, UserHostAddress = allIpAddresses, RouteData = _routeDataConverter.ConvertRouteData(filterContext.HttpContext.Request.RequestContext .RouteData.Values) }; _exceptionLogger.LogException(filterContext.Exception, loggingModel); if (!filterContext.ExceptionHandled) { filterContext.Result = new ViewResult() { ViewName = "ErrorPage" }; filterContext.ExceptionHandled = true; } }
public void LogException(Exception exception, MvcLoggingModel mvcLoggingModel) { if (exception == null) { return; } var errorMessage = new StringBuilder(); errorMessage.AppendLine("\r\n"); if (mvcLoggingModel != null) { errorMessage.AppendLine($"Пользователь: {mvcLoggingModel.UserName}"); errorMessage.AppendLine($"IP-адрес(а): {mvcLoggingModel.UserHostAddress}"); if (mvcLoggingModel.RouteData != null) { if (mvcLoggingModel.RouteData.ContainsKey("controller") && mvcLoggingModel.RouteData.ContainsKey("action")) { errorMessage.AppendLine($"Контроллер: {mvcLoggingModel.RouteData?["controller"]} Метод: {mvcLoggingModel.RouteData?["action"]}"); } } } FillInnerExceptions(errorMessage, exception); Logger.Error(errorMessage.ToString); }
public void Logexception_InputExceptionWithInnerException() { var exception = new Exception("Main_Exception", new Exception("Inner_Exception")); var mvcLoggingModel = new MvcLoggingModel(); _nlogExceptionLogger.LogException(exception, mvcLoggingModel); }
public void Logexception_InputMvcLoggingModelRouteDataIsNull() { var exception = new Exception("Test Exception"); var mvcLoggingModel = new MvcLoggingModel(); _nlogExceptionLogger.LogException(exception, mvcLoggingModel); }
public void Logexception_InputMvcLoggingModelRouteDataWithWrongKeys() { var exception = new Exception("Test Exception"); var mvcLoggingModel = new MvcLoggingModel() { RouteData = new Dictionary <string, object>() { { "fake", "Fake" } } }; _nlogExceptionLogger.LogException(exception, mvcLoggingModel); }
public void Logexception_InputNotNullExceptionAndMvcLoggingModelRouteData() { var exception = new Exception("Test Exception"); var mvcLoggingModel = new MvcLoggingModel() { RouteData = new Dictionary <string, object>() { { "controller", "Controller" }, { "action", "Action" } } }; _nlogExceptionLogger.LogException(exception, mvcLoggingModel); }