public async Task <RequestLogModel> RequestIndiactor(HttpContext httpContext) { // TODO. ASK HOW TO FILL BELOW FIELDS // ActionTypeId - not now // EntityChanges - not now // QueryString - route parameter var requestAddress = httpContext.Request.Path; _stopwatch.Start(); var request = new Entities.RequestLog { IpAddress = httpContext.Connection.RemoteIpAddress.ToString(), Path = requestAddress, Method = httpContext.Request.Method, StartTime = DateTime.Now, }; if (httpContext.Request?.Body != null && !string.IsNullOrWhiteSpace(httpContext.Request.ContentType) && !httpContext.Request.ContentType.StartsWith("multipart/form-data", StringComparison.InvariantCultureIgnoreCase)) { #region Get Request Body request.Body = await new StreamReader(httpContext.Request.Body).ReadToEndAsync(); var injectedRequestStream = new MemoryStream(); var bytesToWrite = Encoding.UTF8.GetBytes(request.Body); injectedRequestStream.Write(bytesToWrite, 0, bytesToWrite.Length); injectedRequestStream.Seek(0, SeekOrigin.Begin); httpContext.Request.Body = injectedRequestStream; #endregion } request.RequestSize = httpContext.Request.ContentLength; if (!IgnoreRequest(requestAddress)) { _dbContext.RequestLogs.Add(request); await _dbContext.SaveChangesAsync(); } return(new RequestLogModel { Id = request.Id, Body = request.Body, IpAddress = request.IpAddress, Method = request.Method, Path = request.Path, StartTime = request.StartTime, UserId = request.UserId, SavedLog = request, }); }
public async Task SaveDataChanges(object businessRepository, string entityTypeName) { var entityType = await _metadataDbContext.EntityTypes .Include(x => x.Properties) .Include("Properties.GeneralUsageCategory") .Where(x => x.Name == entityTypeName) .FirstOrDefaultAsync(); var dataChanges = (businessRepository as DbContext).ChangeTracker .Entries() .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added || x.State == EntityState.Deleted) .Select(x => new { Entity = x.Entity, Action = x.State }) .ToList(); foreach (var dataChange in dataChanges) { // Note. This history does not support primary kyes which are not int E.g. GUID, String var primaryKey = entityType.Properties.Where(x => x.GeneralUsageCategory.Name == "PrimaryKey").FirstOrDefault(); var data = new Entities.DataLog { EntityId = entityType.Id, // Consider that data always has primary key DataId = dataChange.Entity.GetType().GetProperty(primaryKey.Name).GetValue(dataChange.Entity)?.ToString(), Body = JsonConvert.SerializeObject(dataChange.Entity), DataRequestAction = (DataRequestAction)dataChange.Action, RequestLogId = ((RequestLogModel)_httpContextAccessor.HttpContext.Items["RequestLog"]).Id }; _lobToolsDbContext.DataLogs.Add(data); } await _lobToolsDbContext.SaveChangesAsync(); }