public void ConfigFileChangeTriggersReload() { //var configPath = Assembly.GetExecutingAssembly().Location + ".config"; //var configPath = @"D:\Code\Diagnostics\EssentialDiagnostics\Essential.Diagnostics.Tests\bin\Debug\Essential.Diagnostics.Tests.dll.config"; var configPath = ConfigUtility.GetConfigDirFromTestRunDirectory(TestContext.TestRunDirectory); Console.WriteLine("Config path: '{0}'", configPath); TraceSource source = new TraceSource("inmemory2Source"); var listener1 = source.Listeners.OfType<InMemoryTraceListener>().First(); listener1.Clear(); // Verify starting settings source.TraceEvent(TraceEventType.Information, 1, "A"); var events1 = listener1.GetEvents(); Assert.AreEqual(666, listener1.Limit); Assert.AreEqual(1, events1.Length); using (var file = new FileResetScope(configPath)) { using (var configMonitor = new TraceConfigurationMonitor(configPath, false)) { configMonitor.Start(); var changedConfigText = file.OriginalText.Replace("666", "777"); File.WriteAllText(configPath, changedConfigText); // Allow time for async watcher to trigger Thread.Sleep(100); source.TraceEvent(TraceEventType.Information, 1, "B"); var listener2 = source.Listeners.OfType<InMemoryTraceListener>().First(); var events2 = listener2.GetEvents(); System.Threading.Thread.Sleep(500); Assert.AreEqual(777, listener2.Limit); Assert.AreEqual(1, events2.Length); } } }
public void ColoredConfigParametersRefreshCorrectly() { TraceSource source = new TraceSource("colored1Source"); var listener1 = source.Listeners.OfType<ColoredConsoleTraceListener>().First(); // Verify values are correct before changing Assert.AreEqual("{DateTime} {EventType}: {Message}", listener1.Template); Assert.AreEqual(ConsoleColor.DarkBlue, listener1.GetConsoleColor(TraceEventType.Critical)); Assert.IsFalse(listener1.ConvertWriteToEvent); var configPath = ConfigUtility.GetConfigDirFromTestRunDirectory(TestContext.TestRunDirectory); Debug.WriteLine("configPath=" + configPath); using (var file = new FileResetScope(configPath)) { var doc = XDocument.Parse(file.OriginalText); var configuration = doc.Root; //.Element("configuration"); var systemDiagnostics = configuration.Element("system.diagnostics"); var sharedListeners = systemDiagnostics.Element("sharedListeners"); var listenerConfig = sharedListeners.Elements().First(x => x.Attribute("name").Value == "colored1"); Assert.AreEqual("{DateTime} {EventType}: {Message}", listenerConfig.Attribute("template").Value, "config not loaded correctly."); listenerConfig.SetAttributeValue("template", "TEST {Message}"); listenerConfig.SetAttributeValue("criticalColor", ConsoleColor.Cyan); listenerConfig.SetAttributeValue("convertWriteToEvent", true); doc.Save(configPath); Trace.Refresh(); source = new TraceSource("colored1Source"); var listener2 = source.Listeners.OfType<ColoredConsoleTraceListener>().First(); Assert.AreEqual("TEST {Message}", listener2.Template); Assert.AreEqual(ConsoleColor.Cyan, listener2.GetConsoleColor(TraceEventType.Critical)); Assert.IsTrue(listener2.ConvertWriteToEvent); } }
public void ColoredInitializeDataRefreshCorrectly() { TraceSource source = new TraceSource("colored1Source"); var listener1 = source.Listeners.OfType<ColoredConsoleTraceListener>().First(); // Verify values are correct before changing Assert.IsFalse(listener1.UseErrorStream); var configPath = ConfigUtility.GetConfigDirFromTestRunDirectory(TestContext.TestRunDirectory); using (var file = new FileResetScope(configPath)) { var doc = XDocument.Parse(file.OriginalText); var configuration = doc.Root; //.Element("configuration"); var systemDiagnostics = configuration.Element("system.diagnostics"); var sharedListeners = systemDiagnostics.Element("sharedListeners"); var listenerConfig = sharedListeners.Elements().First(x => x.Attribute("name").Value == "colored1"); listenerConfig.SetAttributeValue("initializeData", true); doc.Save(configPath); Trace.Refresh(); var listener2 = source.Listeners.OfType<ColoredConsoleTraceListener>().First(); Assert.IsTrue(listener2.UseErrorStream); Assert.AreNotSame(listener1, listener2); } }