public void OnActionExecuted(ActionExecutedContext context) { _stopWatch.Stop(); _log.ExcuteMilliseconds = _stopWatch.ElapsedMilliseconds; if (context.Exception != null && context.Exception.GetType() != typeof(CustomException)) { _log.LogType = LogTypeEnum.Error; _log.ErrorMsg = context.Exception.ToString(); } _logService.Add(_log); }
private async Task WriteLog(ActionExecutingContext context, string input, APIResult result) { try { //if (string.IsNullOrEmpty(context.HttpContext.User.Identity.Name)) // return; var action = (context.ActionDescriptor as ControllerActionDescriptor); var desc = action.ControllerTypeInfo.GetCustomAttributes(typeof(DescriptionAttribute), true).Select(f => (DescriptionAttribute)f).FirstOrDefault(); if (desc == null) { return; } if (!ActionInfo.Exist(action.ActionName)) { return; } var model = new SystemLog { UserId = context.HttpContext.User.Identity.Name, UserName = context.HttpContext.User.FindFirstValue("UserName"), Controller = action.ControllerName, Action = action.ActionName, ActionName = ActionInfo.GetKeyName(action.ActionName), ActionResult = string.Format("{0}|状态码:{1}", result.Message, result.Code), KeyText = input, ControllerName = desc.Description, IP = context.HttpContext.Connection.RemoteIpAddress.ToString() }; await system.Add(model); } catch (Exception ex) { logger.LogError(ex, "系统日志记录异常"); } }
public async Task <APIResult> Add(SystemLog add) { return(await service.Add(add)); }