public override void Handle(ExceptionHandlerContext context)
        {
            var timer = ((Stopwatch)context.Request.Properties["logtimer"]);

            timer.Stop();

            var exception     = context.Exception;
            var httpException = exception as HttpException;
            var logMessage    = new Resources.LogMessage
            {
                RequestUri    = context.Request.RequestUri.LocalPath,
                RequestMethod = context.Request.Method.Method,
                ElapsedTime   = timer.ElapsedMilliseconds
            };

            if (httpException != null)
            {
                context.Result = new Resources.WebErrorMessage(context.Request, (HttpStatusCode)httpException.GetHttpCode(), httpException.Message);
                return;
            }

            logMessage.Message    = exception.StackTrace;
            logMessage.StatusCode = (int)HttpStatusCode.InternalServerError;
            _logger.Error(logMessage.ToString());

            context.Result = new Resources.WebErrorMessage(context.Request, HttpStatusCode.InternalServerError, exception.Message);
        }
예제 #2
0
        private string GetMessage(HttpActionExecutedContext context, Boolean toJson = false)
        {
            var logMsg = new Resources.LogMessage()
            {
                StatusCode    = (int)context.Response.StatusCode,
                RequestMethod = context.Request.Method.Method,
                RequestUri    = context.Request.RequestUri.LocalPath,
                Message       = context.Response.StatusCode.ToString(),
                ElapsedTime   = _elapsedTime
            };

            if (toJson)
            {
                return(logMsg.ToJson());
            }
            else
            {
                return(logMsg.ToString());
            }
        }