public void InsertLog(LinLog linlog) { linlog.Time = DateTime.Now; linlog.UserName = _currentUser.UserName; linlog.UserId = _currentUser.Id ?? 0; _linLogRepository.Insert(linlog); }
public async Task CreateAsync(LinLog linlog) { linlog.CreateTime = DateTime.Now; linlog.Username = CurrentUser.UserName; linlog.UserId = CurrentUser.Id ?? 0; await _linLogRepository.InsertAsync(linlog); }
public void AddLog(int id, string?userName, string template, string?auth) { var message = TemplateReplace(template); var httpContext = _httpContextAccessor.HttpContext; var log = new LinLog { UserId = id, UserName = userName, Method = httpContext.Request.Method, Path = httpContext.Request.Path, StatusCode = httpContext.Response.StatusCode, Time = DateTime.Now, Message = message, Authority = auth }; _linContext.Add(log); _unitOfWork.Save(); }
public override void OnActionExecuted(ActionExecutedContext context) { //当方法或控制器上存在DisableAuditingAttribute特性标签时,不记录日志 if (context.ActionDescriptor is ControllerActionDescriptor d && d.MethodInfo.IsDefined(typeof(DisableAuditingAttribute), true) || context.Controller.GetType().IsDefined(typeof(DisableAuditingAttribute), true) ) { base.OnActionExecuted(context); return; } LoggerAttribute loggerAttribute = context.ActionDescriptor.EndpointMetadata.OfType <LoggerAttribute>().FirstOrDefault(); var linLog = new LinLog { Path = context.HttpContext.Request.Path, Method = context.HttpContext.Request.Method, StatusCode = context.HttpContext.Response.StatusCode, UserId = _currentUser.Id ?? 0, Username = _currentUser.UserName }; if (loggerAttribute != null) { linLog.Message = this.parseTemplate(loggerAttribute.Template, _currentUser, context.HttpContext.Request, context.HttpContext.Response); } else { linLog.Message = $"访问{linLog.Path}"; } LinCmsAuthorizeAttribute linCmsAttribute = context.ActionDescriptor.EndpointMetadata.OfType <LinCmsAuthorizeAttribute>().FirstOrDefault(); if (linCmsAttribute != null) { linLog.Authority = $"{linCmsAttribute.Module} {linCmsAttribute.Permission}"; } _logRepository.Insert(linLog); base.OnActionExecuted(context); }
public override void OnActionExecuted(ActionExecutedContext context) { Stopwatch.Stop(); //当方法或控制器上存在DisableAuditingAttribute特性标签时,不记录日志 if (context.ActionDescriptor is ControllerActionDescriptor d && d.MethodInfo.IsDefined(typeof(DisableAuditingAttribute), true) || context.Controller.GetType().IsDefined(typeof(DisableAuditingAttribute), true) ) { base.OnActionExecuted(context); return; } LinLog linLog = new LinLog() { Method = context.HttpContext.Request.Method, Path = context.HttpContext.Request.Path, StatusCode = context.HttpContext.Response.StatusCode, OtherMessage = $"参数:{ActionArguments}\n耗时:{Stopwatch.Elapsed.TotalMilliseconds} 毫秒" }; ControllerActionDescriptor auditActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor; AuditingLogAttribute auditingLogAttribute = auditActionDescriptor.GetCustomAttribute <AuditingLogAttribute>(); if (auditingLogAttribute != null) { linLog.Message = auditingLogAttribute.Template; } LinCmsAuthorizeAttribute linCmsAttribute = auditActionDescriptor.GetCustomAttribute <LinCmsAuthorizeAttribute>(); if (linCmsAttribute != null) { linLog.Authority = linCmsAttribute.Permission; } base.OnActionExecuted(context); if (context.Result is ObjectResult objectResult && objectResult.Value != null) { if (objectResult.Value.ToString().Contains("ErrorCode")) { ResultDto resultDto = JsonConvert.DeserializeObject <ResultDto>(objectResult.Value.ToString()); resultDto.Request = LinCmsUtils.GetRequest(context.HttpContext); context.Result = new JsonResult(resultDto); if (linLog.Message.IsNullOrEmpty()) { linLog.Message = resultDto.Msg?.ToString(); } } } linLog.Message += $"{_currentUser.UserName}访问{context.HttpContext.Request.Path},耗时:{Stopwatch.Elapsed.TotalMilliseconds} 毫秒"; _logService.InsertLog(linLog); //记录文本日志 _logger.LogInformation(JsonConvert.SerializeObject(linLog)); //MiniProfiler.Current.CustomTiming($"OnActionExecuted ->", str); }