public async Task GenerateInvoices() { await AuditScope.LogAsync("Invoice:Generate", new { Message = "Start Generating Invoice" }); var supplierIds = await _unitOfWork.SupplierRepository.GetAll() .Select(s => s.Id) .ToListAsync(); foreach (var supplierId in supplierIds) { try { await AuditScope.LogAsync("Invoice:Generate", new { Message = $"Generating invoice for Supplier ID : {supplierId}" }); await GenerateInvoice(supplierId); await AuditScope.LogAsync("Invoice:Generate", new { Message = $"Successfully generated invoice for Supplier ID : {supplierId}" }); } catch (Exception exception) { await AuditScope.LogAsync("Invoice:Generate", new { Message = $"Unable to generate invoice for Supplier ID : {supplierId}", exception }); } } }
public async Task Test_AuditScope_Log_Async() { Audit.Core.Configuration.SystemClock = new MyClock(); var evs = new List <AuditEvent>(); Audit.Core.Configuration.Setup() .Use(x => x.OnInsertAndReplace(ev => { evs.Add(ev); })) .WithCreationPolicy(EventCreationPolicy.InsertOnEnd); await AuditScope.LogAsync("test", new { field1 = "one" }); Assert.AreEqual(1, evs.Count); Assert.AreEqual("test", evs[0].EventType); Assert.AreEqual("one", evs[0].CustomFields["field1"]); }
public async Task Test_StartAndSave_Async() { var provider = new Mock <AuditDataProvider>(); provider.Setup(p => p.Serialize(It.IsAny <string>())).CallBase(); var eventType = "event type"; await AuditScope.LogAsync(eventType, new { ExtraField = "extra value" }); await AuditScope.CreateAndSaveAsync(eventType, new { Extra1 = new { SubExtra1 = "test1" }, Extra2 = "test2" }, provider.Object); provider.Verify(p => p.InsertEventAsync(It.IsAny <AuditEvent>()), Times.Once); provider.Verify(p => p.ReplaceEvent(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Never); provider.Verify(p => p.ReplaceEventAsync(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Never); }
public async Task Test_ScopeActionsStress_Async() { int counter = 0; int counter2 = 0; int counter3 = 0; int MAX = 200; Audit.Core.Configuration.Setup() .UseDynamicProvider(_ => _.OnInsert(ev => { System.Threading.Interlocked.Increment(ref counter); })) .WithCreationPolicy(EventCreationPolicy.InsertOnEnd) .WithAction(_ => _.OnEventSaving(ev => { System.Threading.Interlocked.Increment(ref counter2); })) .WithAction(_ => _.OnScopeCreated(ev => { System.Threading.Interlocked.Increment(ref counter3); })); var tasks = new List <Task>(); for (int i = 0; i < MAX; i++) { tasks.Add(Task.Factory.StartNew(async() => { await AuditScope.LogAsync("LoginSuccess", new { username = "******", id = i }); Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, ev => { //do nothing, just bother var d = ev.Event.Duration * 1234567; }); await AuditScope.CreateAndSaveAsync("LoginFailed", new { username = "******", id = i * -1 }); })); } await Task.WhenAll(tasks.ToArray()); await Task.Delay(1000); Assert.AreEqual(MAX * 2, counter); Assert.AreEqual(MAX * 2, counter2); Assert.AreEqual(MAX * 2, counter3); }