public void TestContext() { var queue = new Loggers.Queue(); var log = new Log(this); using (Log.RegisterInstance( new Instance() { Properties = new[] { "Environment.MachineName", "Environment.UserName", "Environment.UserDomainName", "Environment.CurrentDirectory", "Environment.OSVersion", "Environment.ProcessorCount", "Environment.SystemRoot" }, Logger = queue, Buffer = 0, Synchronous = true } ) ) { log.Info("test"); var evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Environment.MachineName"], Environment.MachineName); Assert.AreEqual(evt["Environment.UserName"], Environment.UserName); Assert.AreEqual(evt["Environment.UserDomainName"], Environment.UserDomainName); Assert.AreEqual(evt["Environment.CurrentDirectory"], Environment.CurrentDirectory); Assert.AreEqual(evt["Environment.OSVersion"], Convert.ToString(Environment.OSVersion)); Assert.AreEqual(evt["Environment.ProcessorCount"], Environment.ProcessorCount); Assert.AreEqual(evt["Environment.SystemRoot"], Environment.GetEnvironmentVariable("SystemRoot")); } }
public void TestContext() { var queue = new Loggers.Queue(); var log = new Log(this); using (Log.RegisterInstance( new Instance() { Properties = new[] { "Thread.ID", "Thread.StackTrace" }, Logger = queue, Buffer = 0, Synchronous = true } ) ) { log.Info("test"); var evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Thread.ID"], System.Threading.Thread.CurrentThread.ManagedThreadId); Assert.IsTrue(((String)evt["Thread.StackTrace"]).Contains("TestContext")); } }
public void TestContext () { var queue = new Loggers.Queue(); var log = new Log(this); using (Log.RegisterInstance( new Instance() { Properties = new[] { "StackFrame.File", "StackFrame.Line", "StackFrame.Type", "StackFrame.Method" }, Logger = queue, Buffer = 0, Synchronous = true } ) ) { var frame = new System.Diagnostics.StackFrame(0, true); log.Info("test"); var evt = queue.Dequeue().Single(); Assert.AreEqual(evt["StackFrame.File"], frame.GetFileName()); Assert.AreEqual(evt["StackFrame.Line"], frame.GetFileLineNumber() + 1); Assert.AreEqual(evt["StackFrame.Type"], frame.GetMethod().DeclaringType.FullName); Assert.AreEqual(evt["StackFrame.Method"], frame.GetMethod().Name); } }
public void TestTracing() { // configure the trace listener var listener = new NLogEx.TraceListener(); Trace.Listeners.Add(listener); // configure the logger var queue = new Loggers.Queue(); using ( Log.RegisterInstance( new Instance() { Properties = new[] { "Event.Type", "Event.Message" }, Synchronous = true, Buffer = 0, Logger = queue } ) ) { Event evt; // informational event Trace.TraceInformation("trace info"); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Type"], EventType.Info); Assert.IsTrue(((String)evt["Event.Message"]).Contains("trace info")); // warning event Trace.TraceWarning("trace warning"); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Type"], EventType.Warning); Assert.IsTrue(((String)evt["Event.Message"]).Contains("trace warning")); // error event Trace.TraceError("trace error"); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Type"], EventType.Error); Assert.IsTrue(((String)evt["Event.Message"]).Contains("trace error")); } Trace.Listeners.Remove(listener); }
public void TestContext() { var queue = new Loggers.Queue(); var log = new Log(this); using (Log.RegisterInstance( new Instance() { Properties = new[] { "Process.ID", "Process.Name", "Process.StartTime", "Process.PeakVirtualMemory", "Process.PeakPagedMemory", "Process.PeakWorkingSetMemory", "Process.ProcessorTime", "Process.PrivilegedProcessorTime", "Process.UserProcessorTime" }, Logger = queue, Buffer = 0, Synchronous = true } ) ) { log.Info("test"); var evt = queue.Dequeue().Single(); using (var process = System.Diagnostics.Process.GetCurrentProcess()) { Assert.AreEqual(evt["Process.ID"], process.Id); Assert.AreEqual(evt["Process.Name"], process.ProcessName); Assert.AreEqual(evt["Process.StartTime"], process.StartTime); Assert.IsTrue((Int64)evt["Process.PeakVirtualMemory"] > 0); Assert.IsTrue((Int64)evt["Process.PeakPagedMemory"] > 0); Assert.IsTrue((Int64)evt["Process.PeakWorkingSetMemory"] > 0); Assert.IsTrue((TimeSpan)evt["Process.ProcessorTime"] > TimeSpan.FromTicks(0)); Assert.IsTrue((TimeSpan)evt["Process.PrivilegedProcessorTime"] > TimeSpan.FromTicks(0)); Assert.IsTrue((TimeSpan)evt["Process.UserProcessorTime"] > TimeSpan.FromTicks(0)); } } }
public void TestGlobalProperties() { // configure the log var log = new Log(this); var queue = new Loggers.Queue(); using ( Log.RegisterInstance( new Instance() { Logger = queue, Synchronous = true, Buffer = 0, Properties = new[] { "Global.Value", "Global.Host" } } ) ) { Event evt; var globalCurrent = globalValue; // initial value log.Info(null); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Global.Value"], globalValue); Assert.AreEqual(evt["Global.Value"], ++globalCurrent); Assert.AreEqual(evt["Global.Host"], Environment.MachineName); // subsequent log.Info(null); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Global.Value"], globalValue); Assert.AreEqual(evt["Global.Value"], ++globalCurrent); Assert.AreEqual(evt["Global.Host"], Environment.MachineName); } }
public void TestBuffering() { var log = new Log(this); var queue = new Loggers.Queue(); var buffer = 10; using ( Log.RegisterInstance( new Instance() { Logger = queue, Synchronous = true, Buffer = buffer, Properties = new[] { "Event.Timestamp", "Event.Time", "Event.Index" } } ) ) { // manual flush { for (Int32 i = 0; i < buffer - 1; i++) { log.InfoEx(new { Index = i }); Assert.IsFalse(queue.Dequeue().Any()); } Log.Flush(); var events = queue.Dequeue().ToList(); Assert.AreEqual(events.Count, buffer - 1); for (Int32 i = 0; i < buffer - 1; i++) { if (i > 0) { Assert.IsTrue((Int64)events[i]["Event.Timestamp"] >= (Int64)events[i - 1]["Event.Timestamp"]); Assert.IsTrue((DateTime)events[i]["Event.Time"] >= (DateTime)events[i - 1]["Event.Time"]); } Assert.AreEqual(events[i]["Event.Index"], i); } } // auto flush { for (Int32 i = 0; i < buffer - 1; i++) { log.InfoEx(new { Index = i }); Assert.IsFalse(queue.Dequeue().Any()); } log.InfoEx(new { Index = buffer - 1 }); var events = queue.Dequeue().ToList(); Assert.AreEqual(events.Count, buffer); for (Int32 i = 0; i < buffer; i++) { if (i > 0) { Assert.IsTrue((Int64)events[i]["Event.Timestamp"] >= (Int64)events[i - 1]["Event.Timestamp"]); Assert.IsTrue((DateTime)events[i]["Event.Time"] >= (DateTime)events[i - 1]["Event.Time"]); } Assert.AreEqual(events[i]["Event.Index"], i); } } } }
public void TestFiltering() { var log = new Log(this); var queue = new Loggers.Queue(); var instance = new Instance() { Logger = queue, Synchronous = true, Buffer = 0, Properties = new[] { "Event.Type", "TestLog.ClassValue", "Event.EventValue", "Global.Host" } }; // property inclusion instance.Filter = new Filter() { IncludeProps = new[] { new Filter.Property("Event.Type", "Error"), new Filter.Property("Event.Type", "Warning") } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); log.Warn("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); log.Info("test"); Assert.IsFalse(queue.Dequeue().Any()); } // property exclusion instance.Filter = new Filter() { ExcludeProps = new[] { new Filter.Property("Event.Type", "Error"), new Filter.Property("Event.Type", "Warning") } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.IsFalse(queue.Dequeue().Any()); log.Warn("test"); Assert.IsFalse(queue.Dequeue().Any()); log.Info("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); } instance.Filter = new Filter() { ExcludeProps = new[] { new Filter.Property("Event.Type", "Error"), new Filter.Property("Event.Type", "Warning") } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.IsFalse(queue.Dequeue().Any()); log.Warn("test"); Assert.IsFalse(queue.Dequeue().Any()); log.Info("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); } // property inclusion/exclusion instance.Filter = new Filter() { IncludeProps = new[] { new Filter.Property("Event.Type", "Error"), new Filter.Property("Event.Type", "Warning") }, ExcludeProps = new[] { new Filter.Property("Event.Type", "Warning"), } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); log.Warn("test"); Assert.IsFalse(queue.Dequeue().Any()); log.Info("test"); Assert.IsFalse(queue.Dequeue().Any()); } // source inclusion instance.Filter = new Filter() { IncludeProps = new[] { new Filter.Property("Event.Source", GetType().AssemblyQualifiedName), new Filter.Property("Event.Source", typeof(Int32).AssemblyQualifiedName) } }; using (Log.RegisterInstance(instance)) { log.Info("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); new Log(typeof(Int32)).Info("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); new Log(typeof(String)).Info("test"); Assert.IsFalse(queue.Dequeue().Any()); } // source exclusion instance.Filter = new Filter() { ExcludeProps = new[] { new Filter.Property("Event.Source", GetType().AssemblyQualifiedName), new Filter.Property("Event.Source", typeof(Int32).AssemblyQualifiedName) } }; using (Log.RegisterInstance(instance)) { log.Info("test"); Assert.IsFalse(queue.Dequeue().Any()); new Log(typeof(Int32)).Info("test"); Assert.IsFalse(queue.Dequeue().Any()); new Log(typeof(String)).Info("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); } // source inclusion/exclusion instance.Filter = new Filter() { IncludeProps = new[] { new Filter.Property("Event.Source", GetType().AssemblyQualifiedName), new Filter.Property("Event.Source", typeof(Int32).AssemblyQualifiedName) }, ExcludeProps = new[] { new Filter.Property("Event.Source", typeof(Int32).AssemblyQualifiedName) } }; using (Log.RegisterInstance(instance)) { log.Info("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); new Log(typeof(Int32)).Info("test"); Assert.IsFalse(queue.Dequeue().Any()); new Log(typeof(String)).Info("test"); Assert.IsFalse(queue.Dequeue().Any()); } // non-event property filter instance.Filter = new Filter() { IncludeProps = new[] { new Filter.Property("Event.Message", "test"), } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); log.Error("invalid"); Assert.IsFalse(queue.Dequeue().Any()); } instance.Filter = new Filter() { ExcludeProps = new[] { new Filter.Property("Event.Message", "test"), } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.IsFalse(queue.Dequeue().Any()); log.Error("invalid"); Assert.AreEqual(queue.Dequeue().Count(), 1); } // non-event global property filter instance.Filter = new Filter() { IncludeProps = new[] { new Filter.Property("Global.Host", Environment.MachineName), } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.AreEqual(queue.Dequeue().Count(), 1); } instance.Filter = new Filter() { ExcludeProps = new[] { new Filter.Property("Global.Host", Environment.MachineName), } }; using (Log.RegisterInstance(instance)) { log.Error("test"); Assert.IsFalse(queue.Dequeue().Any()); } }
public void TestEventProperties() { // configure the log var log = new Log(this); var queue = new Loggers.Queue(); var props = new List<String>() { "Event.Type", "Event.Source", "Event.Timestamp", "Event.Time", "Event.Message", "Event.Exception", }; using ( Log.RegisterInstance( new Instance() { Logger = queue, Synchronous = true, Buffer = 0, Properties = props } ) ) { Event evt; // invalid property log.Trace(""); evt = queue.Dequeue().Single(); Assert.IsNull(evt[null]); Assert.IsNull(evt[""]); Assert.IsNull(evt[" "]); Assert.IsNull(evt["Invalid"]); Assert.IsNull(evt["Event.Invalid"]); // property list log.Error(new Exception("test")); evt = queue.Dequeue().Single(); Assert.IsTrue(evt.Names.SequenceEqual(props)); Assert.IsTrue(evt.Properties.Select(p => p.Key).SequenceEqual(props)); Assert.IsTrue(!evt.Values.Any(v => v == null)); Assert.IsTrue(!evt.Properties.Any(p => p.Value == null)); // event type log.Trace(""); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Type"], EventType.Trace); log.Info(""); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["event.type"], EventType.Info); log.Warn(""); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.type"], EventType.Warning); log.Error(""); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["event.Type"], EventType.Error); // event source log.Info(""); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Source"], GetType()); new Log(typeof(String)).Info(""); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Source"], typeof(String)); // event timestamp Int64 oldStamp = 0; log.Info(""); evt = queue.Dequeue().Single(); oldStamp = (Int64)evt["Event.Timestamp"]; Assert.IsTrue(oldStamp != 0); log.Info(""); evt = queue.Dequeue().Single(); Assert.IsTrue((Int64)evt["Event.Timestamp"] > oldStamp); // event time DateTime oldTime; log.Info(""); evt = queue.Dequeue().Single(); oldTime = (DateTime)evt["Event.Time"]; Assert.IsTrue(oldTime > DateTime.UtcNow - TimeSpan.FromMinutes(1)); Assert.IsTrue(oldTime <= DateTime.UtcNow); log.Info(""); evt = queue.Dequeue().Single(); Assert.IsTrue((DateTime)evt["Event.Time"] >= oldTime); // event message log.Info(null); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Event.Message"]); log.Info(""); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Message"], ""); log.Info(" "); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Message"], " "); log.Info("test"); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Message"], "test"); log.Info("test{0}test{1}test", 0, 1); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Message"], "test0test1test"); // event exception log.Trace(null); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Event.Exception"]); log.Info(null); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Event.Exception"]); log.Warn((Exception)null); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Event.Exception"]); log.Error((Exception)null); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Event.Exception"]); log.Warn(new Exception("test")); evt = queue.Dequeue().Single(); Assert.AreEqual(((Exception)evt["Event.Exception"]).Message, "test"); Assert.AreEqual(evt["Event.Message"], "test"); log.Error(new Exception("test")); evt = queue.Dequeue().Single(); Assert.AreEqual(((Exception)evt["Event.Exception"]).Message, "test"); Assert.AreEqual(evt["Event.Message"], "test"); } }
public void TestCustomProperties() { // configure the log var log = new Log(this); var queue = new Loggers.Queue(); using ( Log.RegisterInstance( new Instance() { Logger = queue, Synchronous = true, Buffer = 0, Properties = new[] { "Event.Property", "TestContext.Field", "TestContext.Property", "TestContext.Private", "TestContext.Method", } } ) ) { Event evt; // typed properties log.InfoEx(new TestContext() { Field = null, Property = null }); evt = queue.Dequeue().Single(); Assert.IsNull(evt["TestContext.Field"]); Assert.IsNull(evt["TestContext.Property"]); Assert.IsNull(evt["Event.Property"]); log.InfoEx(new TestContext() { Field = "", Property = "" }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["TestContext.Field"], ""); Assert.AreEqual(evt["TestContext.Property"], ""); Assert.IsNull(evt["Event.Property"]); log.InfoEx(new TestContext() { Field = " ", Property = " " }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["TestContext.Field"], " "); Assert.AreEqual(evt["TestContext.Property"], " "); Assert.IsNull(evt["Event.Property"]); log.InfoEx(new TestContext() { Field = "test field", Property = "test prop" }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["TestContext.Field"], "test field"); Assert.AreEqual(evt["TestContext.Property"], "test prop"); Assert.IsNull(evt["Event.Property"]); // anonymous properties (default context) log.InfoEx(new { Property = (String)null }); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Event.Property"]); Assert.IsNull(evt["TestContext.Property"]); log.InfoEx(new { Property = "" }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Property"], ""); Assert.IsNull(evt["TestContext.Property"]); log.InfoEx(new { Property = " " }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Property"], " "); Assert.IsNull(evt["TestContext.Property"]); log.InfoEx(new { Property = "test" }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Property"], "test"); Assert.IsNull(evt["TestContext.Property"]); // anonymous property (valid context) log.InfoEx(new { Context = "TestContext", Property = (String)null }); evt = queue.Dequeue().Single(); Assert.IsNull(evt["TestContext.Property"]); Assert.IsNull(evt["Event.Property"]); log.InfoEx(new { Context = "TestContext", Property = "" }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["TestContext.Property"], ""); Assert.IsNull(evt["Event.Property"]); log.InfoEx(new { Context = "TestContext", Property = " " }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["TestContext.Property"], " "); Assert.IsNull(evt["Event.Property"]); log.InfoEx(new { Context = "TestContext", Property = "test" }); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["TestContext.Property"], "test"); Assert.IsNull(evt["Event.Property"]); } }
public void TestConfiguration() { var log = new Log(this); var queue = new Loggers.Queue(); Event evt = null; // invalid global context AssertException(() => Log.RegisterContext("Global", new Dictionary<String, Func<Object>>() { { "Host", () => "test" } } )); // invalid instance registration AssertException(() => Log.RegisterInstance(null)); AssertException(() => Log.RegisterInstance(new Instance())); AssertException(() => Log.RegisterInstance( new Instance() { Buffer = -1, Logger = new Loggers.Null(), Properties = new[] { "Event.Type" }, } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = null, Properties = new[] { "Event.Type" }, } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = null, } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new String[0], } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { (String)null }, } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "" }, } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { " " }, } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "invalid" }, } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "Event.Type" }, Filter = new Filter() { IncludeProps = new[] { new Filter.Property(null, "test") }, } } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "Event.Type" }, Filter = new Filter() { IncludeProps = new[] { new Filter.Property("", "test") }, } } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "Event.Type" }, Filter = new Filter() { IncludeProps = new[] { new Filter.Property(" ", "test") }, } } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "Event.Type" }, Filter = new Filter() { ExcludeProps = new[] { new Filter.Property(null, "test") }, } } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "Event.Type" }, Filter = new Filter() { ExcludeProps = new[] { new Filter.Property("", "test") }, } } )); AssertException(() => Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Properties = new[] { "Event.Type" }, Filter = new Filter() { ExcludeProps = new[] { new Filter.Property(" ", "test") }, } } )); // default instance registration var instance = new Instance() { Logger = queue, Synchronous = true, Buffer = 0, Properties = new[] { "Event.Type" } }; Log.RegisterInstance(instance); log.Trace("trace"); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Type"], EventType.Trace); Log.UnregisterInstance(instance); log.Trace("trace"); Assert.IsFalse(queue.Dequeue().Any()); // disposable instance registration using ( Log.RegisterInstance( new Instance() { Logger = queue, Synchronous = true, Buffer = 0, Properties = new[] { "Event.Type" } } ) ) { log.Info("info"); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["Event.Type"], EventType.Info); } log.Info("info"); Assert.IsFalse(queue.Dequeue().Any()); // log source type Assert.AreEqual(log.SourceType, GetType()); }
public void TestConcurrency() { // concurrent events with no log Parallel.For(0, ParallelIterations, i => { var log = new Log(this); log.InfoEx( new TestContext() { Field = "TestContextField", Property = "TestContextProperty" }, "TestMessage", null ); } ); // configure the log Loggers.Queue queue; using ( Log.RegisterInstance( new Instance() { Logger = queue = new Loggers.Queue(), Synchronous = true, Buffer = 0, Properties = new[] { "Event.Type", "Event.Source", "Event.Timestamp", "Event.Time", "Event.Message", "Event.Exception", "TestLog.Property", "TestContext.Field", "Global.Host" } } ) ) using ( Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Synchronous = true, Buffer = 0, Properties = new[] { "Event.Type" } } ) ) { this.Property = "LogTestProperty"; // concurrent events through private logs { var logs = new Log[ParallelIterations]; for (Int32 i = 0; i < logs.Length; i++) logs[i] = new Log(this); Parallel.For(0, ParallelIterations, i => { logs[i].ErrorEx( new TestContext() { Field = "TestContextField", Property = "TestContextProperty" }, new Exception("TestMessage") ); } ); var events = queue.Dequeue().ToList(); Assert.AreEqual(events.Count, ParallelIterations); foreach (var evt in events) { Assert.AreEqual(evt["Event.Type"], EventType.Error); Assert.AreEqual(evt["Event.Source"], GetType()); Assert.AreNotEqual(evt["Event.Timestamp"], 0); Assert.IsNotNull(evt["Event.Time"]); Assert.AreEqual(evt["Event.Message"], "TestMessage"); Assert.AreEqual(((Exception)evt["Event.Exception"]).Message, "TestMessage"); Assert.AreEqual(evt["TestLog.Property"], "LogTestProperty"); Assert.AreEqual(evt["TestContext.Field"], "TestContextField"); Assert.AreEqual(evt["Global.Host"], Environment.MachineName); } } // concurrent events through a shared log { var log = new Log(this); Parallel.For(0, ParallelIterations, i => { log.ErrorEx( new TestContext() { Field = "TestContextField", Property = "TestContextProperty" }, new Exception("TestMessage") ); } ); var events = queue.Dequeue().ToList(); Assert.AreEqual(events.Count, ParallelIterations); foreach (var evt in events) { Assert.AreEqual(evt["Event.Type"], EventType.Error); Assert.AreEqual(evt["Event.Source"], GetType()); Assert.AreNotEqual(evt["Event.Timestamp"], 0); Assert.IsNotNull(evt["Event.Time"]); Assert.AreEqual(evt["Event.Message"], "TestMessage"); Assert.AreEqual(((Exception)evt["Event.Exception"]).Message, "TestMessage"); Assert.AreEqual(evt["TestLog.Property"], "LogTestProperty"); Assert.AreEqual(evt["TestContext.Field"], "TestContextField"); Assert.AreEqual(evt["Global.Host"], Environment.MachineName); } } } // concurrent events with configuration changes { var log = new Log(this); var queues = new Loggers.Queue[ParallelIterations]; var instances = new Instance[ParallelIterations]; for (Int32 i = 0; i < ParallelIterations; i++) instances[i] = new Instance() { Logger = queues[i] = new Loggers.Queue(), Buffer = 0, Synchronous = true, Properties = new[] { "Event.Type" } }; Parallel.For(0, ParallelIterations, i => { using ( Log.RegisterInstance( new Instance() { Logger = new Loggers.Null(), Buffer = i % 10, Synchronous = true, Properties = new[] { "Event.Type" } } ) ) log.Info("info"); using (Log.RegisterInstance(instances[i])) log.Info("info"); Assert.IsTrue(queues[i].Dequeue().Count() >= 1); } ); } // async event causal ordering using ( Log.RegisterInstance( new Instance() { Logger = queue = new Loggers.Queue(), Buffer = 0, Synchronous = false, Properties = new[] { "Event.Timestamp", "Event.Time", "Event.Index" } } ) ) { var log = new Log(this); for (Int32 i = 0; i < ParallelIterations; i++) log.InfoEx(new { Index = i }); Thread.Sleep(100); var events = queue.Dequeue().ToList(); Assert.AreEqual(events.Count, ParallelIterations); for (Int32 i = 0; i < ParallelIterations; i++) { if (i > 0) { Assert.IsTrue((Int64)events[i]["Event.Timestamp"] >= (Int64)events[i - 1]["Event.Timestamp"]); Assert.IsTrue((DateTime)events[i]["Event.Time"] >= (DateTime)events[i - 1]["Event.Time"]); } Assert.AreEqual(events[i]["Event.Index"], i); } } Log.Flush(); }
public void TestSourceProperties() { // configure the log var queue = new Loggers.Queue(); using ( Log.RegisterInstance( new Instance() { Logger = queue, Synchronous = true, Buffer = 0, Properties = new[] { "TestLog.Property" } } ) ) { Event evt; // default value new Log(this).Info("test"); evt = queue.Dequeue().Single(); Assert.IsNull(evt["TestLog.Property"]); // valid value this.Property = "value"; new Log(this).Info("test"); evt = queue.Dequeue().Single(); Assert.AreEqual(evt["TestLog.Property"], "value"); // typed external value this.Property = null; new Log(GetType(), new TestContext() { Property = "test" }).Info("test"); Assert.AreEqual(evt["TestLog.Property"], "value"); // anonymous external value this.Property = null; new Log(GetType(), new { Property = "test" }).Info("test"); Assert.AreEqual(evt["TestLog.Property"], "value"); } }
public void TestContext() { var queue = new Loggers.Queue(); var log = new Log(this); var evt = (Event)null; using (Log.RegisterInstance( new Instance() { Properties = new[] { "Transaction.LocalID", "Transaction.GlobalID", "Transaction.Started", "Transaction.State" }, Logger = queue, Buffer = 0, Synchronous = true } ) ) { // default transaction state log.Info("test"); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Transaction.LocalID"]); Assert.IsNull(evt["Transaction.GlobalID"]); Assert.IsNull(evt["Transaction.Started"]); Assert.IsNull(evt["Transaction.State"]); // running transaction state using (var txn = new TransactionScope()) log.Info("test"); evt = queue.Dequeue().Single(); Assert.IsNotNull(evt["Transaction.LocalID"]); Assert.IsNull(evt["Transaction.GlobalID"]); Assert.IsTrue((DateTime)evt["Transaction.Started"] <= DateTime.UtcNow); Assert.AreEqual(evt["Transaction.State"], TransactionStatus.Active); // committed transaction state using (var txn = new CommittableTransaction()) { Transaction.Current = txn; txn.Commit(); log.Info("test"); Transaction.Current = null; } evt = queue.Dequeue().Single(); Assert.IsNotNull(evt["Transaction.LocalID"]); Assert.IsNull(evt["Transaction.GlobalID"]); Assert.IsTrue((DateTime)evt["Transaction.Started"] <= DateTime.UtcNow); Assert.AreEqual(evt["Transaction.State"], TransactionStatus.Committed); // aborted transaction state using (var txn = new TransactionScope()) { Transaction.Current.Rollback(); log.Info("test"); } evt = queue.Dequeue().Single(); Assert.IsNotNull(evt["Transaction.LocalID"]); Assert.IsNull(evt["Transaction.GlobalID"]); Assert.IsTrue((DateTime)evt["Transaction.Started"] <= DateTime.UtcNow); Assert.AreEqual(evt["Transaction.State"], TransactionStatus.Aborted); // globally-enlisted transaction state using (var txn = new TransactionScope()) using (var connect1 = new SqlConnection("Data Source=.;Integrated Security=true;")) using (var connect2 = new SqlConnection("Data Source=.;Integrated Security=true;")) { connect1.Open(); connect2.Open(); log.Info("test"); } evt = queue.Dequeue().Single(); Assert.IsNotNull(evt["Transaction.LocalID"]); Assert.IsNotNull(evt["Transaction.GlobalID"]); Assert.IsTrue((DateTime)evt["Transaction.Started"] <= DateTime.UtcNow); Assert.AreEqual(evt["Transaction.State"], TransactionStatus.Active); // null transaction state log.Info("test"); evt = queue.Dequeue().Single(); Assert.IsNull(evt["Transaction.LocalID"]); Assert.IsNull(evt["Transaction.GlobalID"]); Assert.IsNull(evt["Transaction.Started"]); Assert.IsNull(evt["Transaction.State"]); } }