public void Log(FuneralNotice notice, AuditState newState, string error = null) { notice = notice ?? CreateEmptyNotice(); var audit = new AuditFuneralNotice(notice, newState) {Error = error}; if (CanLogToDB(newState)) { AddAuditNotice(audit); } if (error == null) //there is no exception { if (CanLogToElmah(newState)) { string json = JsonConvert.SerializeObject(audit, Formatting.Indented); new Exception(json).LogToElmah(); } } _lastAudit = audit; }
public void t03_add_notice_and_auditlog() { AuditFuneralNotice audit; using (NoticesContext ctx = new NoticesContext()) { FuneralNotice notice = noticeBuilder.WithId(4); ctx.FuneralNotices.Add(notice); audit = new AuditFuneralNotice(notice, AuditState.SavedToDb); ctx.AuditLogFuneralNotices.Add(audit); int result = ctx.SaveChanges(); Assert.IsTrue(result == 2); } //read using (NoticesContext ctx = new NoticesContext()) { audit = ctx.AuditLogFuneralNotices.FirstOrDefault(x => x.Id == 1); } Assert.IsTrue(audit.Id == 1); }
private bool IsSameAsLastAuditNotice(AuditFuneralNotice audit) { if (audit.Equals(_lastAudit)) return true; var lastDbAudit = _ctx.AuditLogFuneralNotices.GetLast(audit.FuneraNoticeId); if (audit.Equals(lastDbAudit)) return true; return false; }
private void AddAuditNotice(AuditFuneralNotice audit) { //prevent adding the same message twice if (!IsSameAsLastAuditNotice(audit)) _ctx.AuditLogFuneralNotices.Add(audit); //_ctx.SaveChanges(); -> performance }
public void t06_audits_verify_getLast() { AuditFuneralNotice result; using (NoticesContext ctx = new NoticesContext()) { FuneralNotice notice = noticeBuilder .WithId(6); var audit1 = new AuditFuneralNotice(notice, AuditState.Creating); var audit2 = new AuditFuneralNotice(notice, AuditState.IndexingFailed); ctx.FuneralNotices.Add(notice); ctx.AuditLogFuneralNotices.AddRange(new[] { audit1, audit2 }); ctx.SaveChanges(); } using (NoticesContext ctx = new NoticesContext()) { result = ctx.AuditLogFuneralNotices.GetLast(6); } Assert.That(result.AuditState, Is.EqualTo(AuditState.IndexingFailed) ); }