/// <summary> /// 记录日志 /// </summary> public void Record(Account account, string loginType) { if (account == null) { throw new ArgumentNullException("登录用户不能为空"); } //获取操作的客户端IP var clientIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress?.ToString(); if (string.IsNullOrEmpty(clientIp)) { clientIp = "127.0.0.1"; } //创建日志对象 var logging = new Logging(account, LoggingType.Login, loginType, clientIp) { Remark = "登录成功" }; //验证数据聚合有效性 var result = logging.Valid(); if (!result.Success) { return; } else { //添加到数据库中 _loggingRepository.AddAsync(logging); } }
public async Task <IActionResult> Post() { using (var reader = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true)) { var bodyString = reader.ReadToEnd(); if (string.IsNullOrEmpty(bodyString)) { return(BadRequest("Body is empty")); } var loggingItem = JsonConvert.DeserializeObject <LoggingItem>(bodyString); if (string.IsNullOrEmpty(loggingItem.Id)) { loggingItem.Id = Guid.NewGuid().ToString(); } var insertResult = await _loggingRepository.AddAsync(loggingItem); return(insertResult.IsSuccessful ? (IActionResult)Ok(insertResult.Result) : BadRequest()); } }
public async Task Invoke(HttpContext context) { bool success = true; string message = string.Empty; Stopwatch sw = Stopwatch.StartNew(); try { context.Response.OnStarting(() => { // Stop the timer information and calculate the time sw.Stop(); var responseTimeForCompleteRequest = sw.ElapsedMilliseconds; // Add the Response time information in the Response headers. context.Response.Headers["X-Response-Time-ms"] = responseTimeForCompleteRequest.ToString(); return(Task.CompletedTask); }); await _next.Invoke(context); // sw.Stop(); int?statusCode = context.Response?.StatusCode; LogEventLevel level = statusCode > 499 ? LogEventLevel.Error : LogEventLevel.Information; if (level == LogEventLevel.Information && !context.Request.Path.ToString().ToLower().Contains("logging")) { Logging logging = new Logging(); logging.RequestMethod = context.Request.Method; logging.Location = $"{context.Request.Scheme}://{context.Request.Host}"; logging.RequestPath = context.Request.Path.ToString(); logging.StatusCode = statusCode.Value; logging.TimeElapsed = sw.Elapsed.TotalMilliseconds; logging.Message = "Request success"; logging.Ip = context.Connection.RemoteIpAddress.ToString(); logging.CreatedDate = DateTime.Now; await loggingRepository.AddAsync(logging); } } catch (Exception ex) { ExceptionMessageModel messageDetail = ReadException(ex.Message); _logger.LogError(10000, ex, ex.Message); message = messageDetail.Message; context.Response.StatusCode = 400; success = false; Logging logging = new Logging(); logging.RequestMethod = context.Request.Method; logging.Location = $"{context.Request.Scheme}://{context.Request.Host}"; logging.RequestPath = context.Request.Path.ToString(); logging.StatusCode = 400; logging.TimeElapsed = sw.Elapsed.TotalMilliseconds; logging.Message = message; logging.CreatedDate = DateTime.Now; logging.Ip = context.Connection.RemoteIpAddress.ToString(); await loggingRepository.AddAsync(logging); } if (!context.Response.HasStarted) { context.Response.ContentType = "application/json"; ApiResponse response = new ApiResponse(success, context.Response.StatusCode, message); string json = JsonConvert.SerializeObject(response); await context.Response.WriteAsync(json); } }