public void LogOperations(string user) { var emailUsuarioLogado = user; var stateAdded = new[] { EntityState.Added }; var statesModifiedAndDeleted = new[] { EntityState.Modified, EntityState.Deleted }; var changesToLogAdded = base.ChangeTracker.Entries().Where(x => stateAdded.Contains(x.State)).ToList(); var changesToLogModifiedAndDeleted = base.ChangeTracker.Entries().Where(x => statesModifiedAndDeleted.Contains(x.State)).ToList(); if (changesToLogModifiedAndDeleted.Any()) { foreach (var change in changesToLogModifiedAndDeleted) { LogContext.LogOperation(this, change, change.State, emailUsuarioLogado); } } if (changesToLogAdded.Any()) { base.SaveChanges(); foreach (var change in changesToLogAdded) { LogContext.LogOperation(this, change, EntityState.Added, emailUsuarioLogado); } } }
private void LogOperations() { var cp = (ClaimsPrincipal)System.Threading.Thread.CurrentPrincipal; string emailUsuarioLogado = string.Empty; var claim = cp.FindFirst("emails"); if (claim != null && !string.IsNullOrEmpty(claim.Value)) { emailUsuarioLogado = claim.Value; } else { emailUsuarioLogado = "PayLendFO"; } var stateAdded = new[] { EntityState.Added }; var statesModifiedAndDeleted = new[] { EntityState.Modified, EntityState.Deleted }; var changesToLogAdded = context.ChangeTracker.Entries().Where(x => stateAdded.Contains(x.State)).ToList(); var changesToLogModifiedAndDeleted = context.ChangeTracker.Entries().Where(x => statesModifiedAndDeleted.Contains(x.State)).ToList(); if (changesToLogModifiedAndDeleted.Any()) { foreach (var change in changesToLogModifiedAndDeleted) { LogContext.LogOperation(context, change, change.State, emailUsuarioLogado); } } if (changesToLogAdded.Any()) { this.context.SaveChanges(); foreach (var change in changesToLogAdded) { LogContext.LogOperation(context, change, EntityState.Added, emailUsuarioLogado); } } }