public void Test_FileDataProvider_Error() { var loop = new Loop() { Id = 1 }; loop.Inner = loop; var fdp = new FileDataProvider() { DirectoryPath = _directory, FilenameBuilder = x => x.EventType }; Configuration.Setup().UseFileLogProvider(_ => _ .Directory(_directory) .FilenameBuilder(x => x.EventType) .JsonSettings(new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Error })); var guid = "x" + Guid.NewGuid().ToString(); try { AuditScope.CreateAndSave(guid, loop); Assert.Fail("Should not get here. JsonSettings not respected?"); } catch (JsonSerializationException ex) { Assert.IsTrue(ex.Message.ToLower().Contains("loop detected")); } }
public void Test_FileDataProvider_Loop() { var loop = new Loop() { Id = 1 }; loop.Inner = loop; var fdp = new FileDataProvider() { DirectoryPath = _directory, FilenameBuilder = x => x.EventType }; Configuration.DataProvider = fdp; var guid = "x" + Guid.NewGuid().ToString(); AuditScope.CreateAndSave(guid, loop); var ev = fdp.GetEvent(Path.Combine(_directory, guid)); Assert.IsNotNull(ev); Assert.AreEqual(guid, ev.EventType); Assert.AreEqual(2, ev.CustomFields.Count); Assert.AreEqual(1, ev.CustomFields["Id"]); Assert.AreEqual(JObject.Parse("{\"Id\": 1}").ToString(), ev.CustomFields["Inner"].ToString()); }
public void Test_StartAndSave() { var provider = new Mock <AuditDataProvider>(); provider.Setup(p => p.Serialize(It.IsAny <string>())).CallBase(); var eventType = "event type"; AuditScope.CreateAndSave(eventType, new { ExtraField = "extra value" }); AuditScope.CreateAndSave(eventType, new { Extra1 = new { SubExtra1 = "test1" }, Extra2 = "test2" }, provider.Object); provider.Verify(p => p.InsertEvent(It.IsAny <AuditEvent>()), Times.Once); provider.Verify(p => p.ReplaceEvent(It.IsAny <object>(), It.IsAny <AuditEvent>()), Times.Never); }
public void Test_ScopeActionsStress() { 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(() => { AuditScope.CreateAndSave("LoginSuccess", new { username = "******", id = i }); Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, ev => { //do nothing, just bother var d = ev.Event.Duration * 1234567; }); AuditScope.CreateAndSave("LoginFailed", new { username = "******", id = i * -1 }); })); } Task.WaitAll(tasks.ToArray()); Assert.AreEqual(MAX * 2, counter); Assert.AreEqual(MAX * 2, counter2); Assert.AreEqual(MAX * 2, counter3); }