Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        public void Logexception_InputExceptionWithInnerException()
        {
            var exception       = new Exception("Main_Exception", new Exception("Inner_Exception"));
            var mvcLoggingModel = new MvcLoggingModel();

            _nlogExceptionLogger.LogException(exception, mvcLoggingModel);
        }
Exemplo n.º 4
0
        public void Logexception_InputMvcLoggingModelRouteDataIsNull()
        {
            var exception       = new Exception("Test Exception");
            var mvcLoggingModel = new MvcLoggingModel();

            _nlogExceptionLogger.LogException(exception, mvcLoggingModel);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }