public void OnException(ExceptionContext context)
        {
            HttpStatusCode statusCode = (context.Exception as WebException != null &&
                                         (HttpWebResponse)(context.Exception as WebException).Response != null) ?
                                        ((HttpWebResponse)(context.Exception as WebException).Response).StatusCode
              : CommonUtitlies.GetErrorCode(context.Exception.GetType());

            string errorMessage       = context.Exception.Message;
            Guid   traceId            = Guid.NewGuid();
            string customErrorMessage = ($"{CommonUtitlies.CustomErrorMessage} {traceId}");
            string stackTrace         = context.Exception.StackTrace;
            string source             = context.HttpContext.Request.Path;
            string host          = context.HttpContext.Request.Host.HasValue ? context.HttpContext.Request.Host.Value : string.Empty;
            string requestMethod = context.HttpContext.Request.Method;
            string userAgent     = ((Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestHeaders)((Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest)context.HttpContext.Request).Headers).HeaderUserAgent.ToString();
            string remoteAddress = context.HttpContext.Request.Host.Value.Contains("localhost") ? "127.0.0.1" : context.HttpContext.Connection.RemoteIpAddress.ToString();
            bool   isProd        = !_currentEnvironment.IsDevelopment();

            ErrorLogViewModel errorLogViewModel = new ErrorLogViewModel()
            {
                ErrorMsg      = errorMessage,
                Source        = source,
                StackTrace    = stackTrace,
                StatusCode    = (int)statusCode,
                Host          = host,
                RequestMethod = requestMethod,
                UserAgent     = userAgent,
                RemoteAddress = remoteAddress
            };

            Task <string> data     = LogAsync(errorLogViewModel);
            bool          isLogged = _loggerBL.Log(errorLogViewModel);
            HttpResponse  response = context.HttpContext.Response;

            response.StatusCode  = (int)HttpStatusCode.InternalServerError;
            response.ContentType = "application/json";
            string result = JsonConvert.SerializeObject(
                new ResponseWrapper <string>()
            {
                Code    = (int)errorLogViewModel.StatusCode,
                Message = errorLogViewModel.ErrorMsg,
                Status  = Status.Error,
                Data    = null
            });

            response.ContentLength = result.Length;
            response.WriteAsync(result);
        }
Beispiel #2
0
        public ErrorLogViewModel ProcessErrorContext(Exception exception, HttpContext context)
        {
            HttpStatusCode statusCode = (exception as WebException != null &&
                                         (HttpWebResponse)(exception as WebException).Response != null) ?
                                        ((HttpWebResponse)(exception as WebException).Response).StatusCode
              : CommonUtitlies.GetErrorCode(exception.GetType());

            string errorMessage = exception.Message;

            //   Guid traceId = Guid.NewGuid();
            //customErrorMessage = ($"{CommonUtitlies.CustomErrorMessage} {traceId}");
            customErrorMessage = ($"{CommonUtitlies.CustomErrorMessage} ");
            string            stackTrace        = exception.StackTrace;
            string            source            = context.Request.Path;
            string            host              = context.Request.Host.HasValue ? context.Request.Host.Value : string.Empty;
            string            requestMethod     = context.Request.Method;
            string            userAgent         = ((Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestHeaders)((Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest)context.Request).Headers).HeaderUserAgent.ToString();
            string            remoteAddress     = context.Request.Host.Value.Contains("localhost") ? "127.0.0.1" : context.Connection.RemoteIpAddress.ToString();
            bool              isProd            = !_currentEnvironment.IsDevelopment();
            string            userEmail         = string.Empty;
            ErrorLogViewModel errorLogViewModel = new ErrorLogViewModel()
            {
                ErrorMsg   = errorMessage,
                IsProd     = false,
                Source     = source,
                StackTrace = stackTrace,
                // TraceId = traceId.ToString(),
                StatusCode    = (int)statusCode,
                Host          = host,
                RequestMethod = requestMethod,
                UserAgent     = userAgent,
                RemoteAddress = remoteAddress,
                UserDetails   = userEmail
            };

            return(errorLogViewModel);
        }