public void when_tracing_activity_then_builds_trace_log() { if (File.Exists(LogFile)) File.Delete(LogFile); var xml = new XmlWriterTraceListener(LogFile, "Xml"); manager.AddListener("*", xml); manager.SetTracingLevel("*", SourceLevels.All); var tracer = Tracer.Get("Foo"); using (tracer.StartActivity("Outer")) { tracer.Info("Hello info from outer"); using (tracer.StartActivity("Inner")) { tracer.Warn("Warn from inner"); Tracer.Get("Foo.Bar").Error("Something failed on another class!"); } } xml.Flush(); System.Threading.Thread.Sleep(1000); xml.Close(); Process.Start(SvcViewer, new FileInfo(LogFile).FullName); }
public void when_activity_has_exception_then_traces_data() { var xmlFile = Path.GetTempFileName(); var listener = new XmlWriterTraceListener(xmlFile); Tracer.Configuration.AddListener("Foo", listener); Tracer.Configuration.SetTracingLevel("Foo", SourceLevels.All); var tracer = Tracer.Get("Foo"); using (var outer = tracer.StartActivity("Outer")) { using (var inner = tracer.StartActivity("Inner")) { tracer.Info("Hey & > 23!"); } try { throw new ArgumentException(); } catch (Exception ex) { ex.Data.Add("Version", "1.0"); ex.Data.Add("Branch", "master"); ex.Data.Add("Commit", "1ab314b"); tracer.Error(ex, "Some error!"); } } Tracer.Configuration.RemoveListener("Foo", listener); listener.Flush(); listener.Dispose(); var xmlns = XNamespace.Get("http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"); var commitFound = false; using (var reader = XmlReader.Create(xmlFile, new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment })) { Assert.Equal(XmlNodeType.Element, reader.MoveToContent()); do { var xml = reader.ReadOuterXml(); var doc = XDocument.Load(new StringReader(xml)); var record = doc.Descendants(xmlns + "TraceRecord").FirstOrDefault(); if (record != null && record.Attribute("Severity") != null && record.Attribute("Severity").Value == "Error") { commitFound = record.Element(xmlns + "DataItems")?.Element(xmlns + "Commit")?.Value == "1ab314b"; } } while (!commitFound && !reader.EOF); } Assert.True(commitFound, "Failed to find traced commit ID exception data."); //Process.Start ("notepad", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\SvcTraceViewer.exe", xml); }
public void when_activity_has_exception_then_traces_data() { var xml = Path.GetTempFileName(); var listener = new XmlWriterTraceListener(xml); Tracer.Configuration.AddListener("Foo", listener); Tracer.Configuration.SetTracingLevel("Foo", SourceLevels.All); var tracer = Tracer.Get("Foo"); using (var outer = tracer.StartActivity("Outer")) { using (var inner = tracer.StartActivity("Inner")) { tracer.Info("Hey & > 23!"); } try { throw new ArgumentException(); } catch (Exception ex) { ex.Data.Add("Version", "1.0"); ex.Data.Add("Branch", "master"); ex.Data.Add("Commit", "1ab314b"); tracer.Error(ex, "Some error!"); } } Tracer.Configuration.RemoveListener("Foo", listener); listener.Flush(); listener.Dispose(); using (var reader = XmlReader.Create(xml, new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment })) { Assert.Equal(XmlNodeType.Element, reader.MoveToContent()); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.NamespaceURI == "http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord") { Assert.True(reader.ReadToDescendant("Commit", "http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord")); Assert.Equal("1ab314b", reader.ReadElementContentAsString()); } } } //Process.Start ("notepad", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\SvcTraceViewer.exe", xml); }
static void Main(string[] args) { _execTimer.Start(); try { Delay(10); LogLongExec("Delay(10)"); Delay(5000); LogLongExec("Delay(5000)"); } catch (Exception ex) { using ( XmlWriterTraceListener log1 = new XmlWriterTraceListener($@"C:\Users\{Environment.UserName}\Pictures\Error.log")) { log1.TraceEvent(new TraceEventCache(), ex.Message, TraceEventType.Error, ex.HResult); log1.Flush(); } } }
static void Main(string[] args) { WriteLine("Option 1"); System.Diagnostics.TraceSource trace = new TraceSource("./1_Trace.log"); trace.TraceEvent(TraceEventType.Error, 123, "Option A: System.Diagnostics.TraceSource trace"); WriteLine("Option 2: writes a file as 'TraceEventType.Error' and source 'Option B...'"); using (System.Diagnostics.XmlWriterTraceListener log1 = new XmlWriterTraceListener("./2_Error.log")) { log1.TraceEvent(new TraceEventCache(), "Option B: System.Diagnostics.XmlWriterTraceListener", TraceEventType.Error, 123); log1.Flush(); } WriteLine("Option 3"); WriteLine("Throws: An unhandled exception of type 'System.Security.SecurityException' occurred in System.dll"); //System.Diagnostics.EventInstance errorEvent = new EventInstance(123, 1, EventLogEntryType.Error); //EventLog.WriteEvent("MyAppErrors", errorEvent, "the message"); WriteLine("Option 4: writes to the EventLog"); EventLog logEntry = new EventLog(); logEntry.Source = "Application"; logEntry.WriteEntry("the message", EventLogEntryType.Error); WriteLine("Option 5: writes a file as 'TraceEventType.Information' and source 'Trace'"); System.Diagnostics.XmlWriterTraceListener listener = new XmlWriterTraceListener("./5_Errror.log"); listener.WriteLine("the log message"); listener.Flush(); listener.Close(); // See Output window: // Error.log: a trace message WriteLine("Option 6: Trace.WriteLine with 'Error.log' as category"); Trace.WriteLine("a trace message", "Error.log"); WriteLine("Done"); ReadLine(); }
public void TraceCompareTest() { TraceEventCache eventCache = new TraceEventCache(); string output1 = null; string output2 = null; using (MemoryStream stream1 = new MemoryStream()) { using (TraceListener tracer = new XmlWriterTraceListener(stream1)) { tracer.TraceData(eventCache, "TestSource", TraceEventType.Information, 1, new { Message = "Test of Message", Id = 123 }); tracer.Flush(); //stream.Position = 0; output1 = Encoding.UTF8.GetString(stream1.ToArray()); Trace.WriteLine(output1); } } using (MemoryStream stream2 = new MemoryStream()) { using (TraceListener tracer = new XmlWriterRollingTraceListener(stream2)) { tracer.TraceData(eventCache, "TestSource", TraceEventType.Information, 1, new { Message = "Test of Message", Id = 123 }); tracer.Flush(); //stream.Position = 0; output2 = Encoding.UTF8.GetString(stream2.ToArray()); Trace.WriteLine(output1); } } Assert.AreEqual<string>(output1, output2); }
public void when_activity_has_exception_then_traces_data () { var xmlFile = Path.GetTempFileName(); var listener = new XmlWriterTraceListener(xmlFile); Tracer.Configuration.AddListener ("Foo", listener); Tracer.Configuration.SetTracingLevel ("Foo", SourceLevels.All); var tracer = Tracer.Get("Foo"); using (var outer = tracer.StartActivity ("Outer")) { using (var inner = tracer.StartActivity("Inner")) { tracer.Info ("Hey & > 23!"); } try { throw new ArgumentException (); } catch (Exception ex) { ex.Data.Add ("Version", "1.0"); ex.Data.Add ("Branch", "master"); ex.Data.Add ("Commit", "1ab314b"); tracer.Error (ex, "Some error!"); } } Tracer.Configuration.RemoveListener ("Foo", listener); listener.Flush (); listener.Dispose (); var xmlns = XNamespace.Get("http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"); var commitFound = false; using (var reader = XmlReader.Create (xmlFile, new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment })) { Assert.Equal (XmlNodeType.Element, reader.MoveToContent ()); do { var xml = reader.ReadOuterXml(); var doc = XDocument.Load(new StringReader(xml)); var record = doc.Descendants(xmlns + "TraceRecord").FirstOrDefault(); if (record != null && record.Attribute("Severity") != null && record.Attribute("Severity").Value == "Error") { commitFound = record.Element(xmlns + "DataItems")?.Element(xmlns + "Commit")?.Value == "1ab314b"; } } while (!commitFound && !reader.EOF); } Assert.True(commitFound, "Failed to find traced commit ID exception data."); //Process.Start ("notepad", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\SvcTraceViewer.exe", xml); //Process.Start (@"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\SvcTraceViewer.exe", xml); }