Exemple #1
0
        public async Task <IActionResult> Post([FromBody] ErrorLogDto value)
        {
            _logger.LogError($"Type: {value.Type} \r\nMessage: {value.Message} \r\nCustom Message: {value.CustomMessage} \r\nPage: {value.Page} \r\nStack:{value.Stack}");

            await _service.SaveErrorLog(value);

            return(Ok());
        }
Exemple #2
0
 /// <summary>
 /// 保存任务异常日志
 /// </summary>
 /// <param name="saveInfo">保存信息</param>
 /// <returns>执行结果</returns>
 public Result SaveErrorLog(SaveErrorLogCmdDto saveInfo)
 {
     if (saveInfo == null || saveInfo.ErrorLogs.IsNullOrEmpty())
     {
         return(Result.FailedResult("没有指定要保存的信息"));
     }
     using (var businessWork = WorkFactory.Create())
     {
         errorLogService.SaveErrorLog(saveInfo.ErrorLogs.Select(c => { c.Id = ErrorLog.GenerateErrorLogId(); return(c.MapTo <ErrorLog>()); }));
         var commitResult = businessWork.Commit();
         return(commitResult.ExecutedSuccess ? Result.SuccessResult("保存成功") : Result.FailedResult("保存失败"));
     }
 }
Exemple #3
0
        public async Task Invoke(HttpContext context, IErrorLogService service)
        {
            try
            {
                await _next(context);
            }
            catch (RulesException re)
            {
                if (context.Response.HasStarted)
                {
                    throw;
                }

                context.Response.StatusCode = 400;
                context.Response.Headers.Add("exception", "validationException");
                var modelState = new ModelStateDictionary();
                re.AddModelStateErrors(modelState);
                var json = JsonConvert.SerializeObject(modelState.Errors(true), _jsonSettings);
                await context.Response.WriteAsync(json);
            }
            catch (CustomMessageException cm)
            {
                if (context.Response.HasStarted)
                {
                    throw;
                }

                object[] args = new object[2];

                args[0] = cm.Message;
                args[1] = cm.StackTrace;

                _logger.LogError(cm, "Exception: Message {0}, Stack {1}", args);

                ErrorLogDto value = new ErrorLogDto();

                value.CustomMessage = cm.ExceptionMessage;
                value.Message       = cm.Message;
                value.Page          = context.Request.Path;
                value.Stack         = cm.StackTrace;
                value.Type          = "Exception";

                await service.SaveErrorLog(value);

                context.Response.StatusCode  = 500;
                context.Response.ContentType = "application/json";
                context.Response.Headers.Add("exception", "messageException");
                var json = JsonConvert.SerializeObject(new { Message = cm.ExceptionMessage }, _jsonSettings);
                await context.Response.WriteAsync(json);
            }
            catch (Exception ex)
            {
                if (context.Response.HasStarted)
                {
                    throw;
                }

                int argCount = 3;

                if (ex.InnerException != null)
                {
                    argCount = 5;
                }

                object[] args = new object[argCount];

                args[0] = ex.Message;
                args[1] = ex.StackTrace;
                args[2] = context.Request.Path;

                if (ex.InnerException != null)
                {
                    args[3] = ex.InnerException.Message;
                    args[4] = ex.InnerException.StackTrace;
                    _logger.LogError(ex, "Exception: Message {0}, Stack {1}, Path {2}. Inner Exception: Message {3}, Stack {4}", args);
                }
                else
                {
                    _logger.LogError(ex, "Exception: Message {0}, Stack {1}, Path {2}", args);
                }

                ErrorLogDto value = new ErrorLogDto();

                value.CustomMessage = ex.Message;
                value.Message       = ex.Message;
                value.Page          = context.Request.Path;
                value.Stack         = ex.StackTrace;
                value.Type          = "Exception";

                await service.SaveErrorLog(value);

                context.Response.StatusCode  = 500;
                context.Response.ContentType = "application/json";
                context.Response.Headers.Add("exception", "messageException");

                object obj;

                if (ex.InnerException != null)
                {
                    obj = new { Message = ex.Message, Stack = ex.StackTrace, Path = context.Request.Path, InnerMessage = ex.InnerException.Message, InnerStack = ex.InnerException.StackTrace }
                }
                ;
                else
                {
                    obj = new { Message = ex.Message, Stack = ex.StackTrace, Path = context.Request.Path }
                };

                var json = JsonConvert.SerializeObject(obj, _jsonSettings);
                await context.Response.WriteAsync(json);
            }
        }
    }