public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { string ip = context.HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault() ?? context.HttpContext.Connection.RemoteIpAddress.ToString(); Guid? userId = null; //if (!token.IsNullOrWhiteSpace()) //{ // userId = (await LoginManager.GetUserIdAsync(token)).GetValueOrDefault(); //} //string controllerName = context.ActionDescriptor.RouteValues["controller"]; string actionName = context.ActionDescriptor.RouteValues["action"]; var auditLogDto = new AuditLogDto { UserId = userId, ServiceName = context.Controller.ToString(), MethodName = actionName, Parameters = ConvertArgumentsToJson(context.ActionArguments), ExecutionTime = DateTime.Now, ClientIpAddress = ip, BrowserInfo = context.HttpContext?.Request?.Headers?["User-Agent"], }; await _auditLogService.InsertAsync(auditLogDto); await base.OnActionExecutionAsync(context, next); }
// CRUD public void Create(AuditLogDto auditLogDto) { var auditLog = Mapper.Map <AuditLog>(auditLogDto); _unitOfWork.AuditLogRepository.Create(auditLog); _unitOfWork.Save(); }
public AuditLogAppService(AuditLogDto auditLogDto) { _userId = auditLogDto.UserId; _entryDateTime = auditLogDto.EntryDateTime; _actionType = auditLogDto.ActionType; _tableName = auditLogDto.TableName; _recordId = auditLogDto.RecordId; }
public void Should_Map() { // Arrange AuditLog auditLog = AuditLogSeedData.UpdateRegion; AuditLogDto expectedDto = AuditLogDtoData.UpdateRegion; // Act AuditLogDto dto = _mapper.Map <AuditLogDto>(auditLog); // Assert dto.Should().BeEquivalentTo(expectedDto); }
// Service Methods public void Audit(string actionType, string tableName, long userId, long recordId) { var auditLogDto = new AuditLogDto { Id = Guid.NewGuid(), ActionType = actionType, EntryDateTime = DateTime.Now, TableName = tableName, UserId = userId, RecordId = recordId }; Create(auditLogDto); }
public async Task Should_Get_All_ByEntryId() { // Arrange Guid entityId = GetEntryId(); // Act HttpResponseMessage responseMessage = await _authenticatedServer .CreateClient() .AuthenticateWith(_staff) .GetAsync(ApiEndpoints.AuditLogsController.Get(entityId, 0, 9999)); // Assert responseMessage.StatusCode.Should().Be(HttpStatusCode.OK); IEnumerable <AuditLogDto> result = await DeserializeResponseMessageAsync <IEnumerable <AuditLogDto> >(responseMessage); result.Count().Should().Be(3); AuditLogDto auditLog = result.Last(); // this should be the create command as the oldest entry auditLog.Type.Should().Be(AuditLogType.Update); // cannot test this here, because sort order does not work due to test seed data being all created at FakeDateTime.UtcNow which all have the time 00:00:00 auditLog.NewValues.Count.Should().BeGreaterThan(0); // prove that mapping dictionaries work }
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { string ip = context.HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault() ?? context.HttpContext.Connection.RemoteIpAddress.ToString(); int? userId = null; //string para = string.Empty; string token = context.HttpContext.Request.Headers["token"]; if (!token.IsNullOrWhiteSpace()) { userId = (await LoginManager.GetUserIdAsync(token)).GetValueOrDefault(); } //foreach (var item in context.ActionArguments) //{ // /*if (item.Key == "token") // { // userId = (await LoginManager.GetUserIdAsync(item.Value.ToString())).Value; // }*/ // para += item.Key + ":" + item.Value + "/"; //} //string controllerName = context.ActionDescriptor.RouteValues["controller"]; string actionName = context.ActionDescriptor.RouteValues["action"]; //await Task.Run(() => Logger.Info($"ip为 {ip} 的用户(userId={userId})在 {DateTime.Now} 执行了 {controllerName}/{actionName} 操作,参数是 {para}")); var auditLogDto = new AuditLogDto { UserId = userId, ServiceName = context.Controller.ToString(), MethodName = actionName, Parameters = ConvertArgumentsToJson(context.ActionArguments), ExecutionTime = DateTime.Now, ClientIpAddress = ip, BrowserInfo = context.HttpContext?.Request?.Headers?["User-Agent"], }; await _auditLogService.InsertAsync(auditLogDto); await base.OnActionExecutionAsync(context, next); }
public async Task OnGetAsync() { AuditLog = await _service.GetAsync(Id); }