static void Main() { File.Delete("TraceOutput.xml"); TraceSource ts = new TraceSource("TestSource"); #if NOCONFIGFILE //ts.Listeners.Add(new EventSchemaTraceListener("TraceOutput.xml", "eventListener", 65536, TraceLogRetentionOption.LimitedCircularFiles, 20480000, 2)); ts.Listeners.Add(new EventSchemaTraceListener("TraceOutput.xml", "eventListener")); ts.Listeners["eventListener"].TraceOutputOptions = TraceOptions.DateTime | TraceOptions.ProcessId | TraceOptions.Timestamp; #endif ts.Switch.Level = SourceLevels.All; string testString = "<Test><InnerElement Val=\"1\" /><InnerElement Val=\"Data\"/><AnotherElement>11</AnotherElement></Test>"; UnescapedXmlDiagnosticData unXData = new UnescapedXmlDiagnosticData(testString); ts.TraceData(TraceEventType.Error, 38, unXData); ts.TraceEvent(TraceEventType.Error, 38, testString); Trace.Listeners.Add(new EventSchemaTraceListener("TraceOutput.xml")); Trace.Write("test", "test"); Trace.Flush(); ts.Flush(); ts.Close(); DisplayProperties(ts); Process.Start("notepad.exe", "TraceOutput.xml"); Console.WriteLine("Press the enter key to exit"); Console.ReadLine(); }
static void Main(string[] args) { string fileName = "TraceOutput.xml"; string event_name = "eventschema_listener"; File.Delete(fileName); //specify the EventSchema trace listener using (var eventschema_listener = new EventSchemaTraceListener(fileName, event_name)) { TraceSource ts = new TraceSource("TestSource"); #if NOCONFIGFILE //ts.Listeners.Add(new EventSchemaTraceListener(fileName, event_name, 65536, TraceLogRetentionOption.LimitedCircularFiles, 20480000, 2)); ts.Listeners.Add(eventschema_listener); ts.Listeners[event_name].TraceOutputOptions = TraceOptions.DateTime | TraceOptions.ProcessId | TraceOptions.Timestamp; #endif ts.Switch.Level = SourceLevels.All; string testString = "<Test><InnerElement Val=\"1\" /><InnerElement Val=\"Data\"/><AnotherElement>11</AnotherElement></Test>"; UnescapedXmlDiagnosticData unXData = new UnescapedXmlDiagnosticData(testString); ts.TraceData(TraceEventType.Error, 38, unXData); ts.TraceEvent(TraceEventType.Error, 38, testString); Trace.Listeners.Add(eventschema_listener); Trace.Write("test eventschema", "test"); Trace.Flush(); ts.Flush(); ts.Close(); DisplayProperties(ts, event_name); Process.Start("notepad++.exe", "TraceOutput.xml"); Console.ReadLine(); } }
private static void XmlTraceSourceSample() { XmlWriterTraceListener xmlTraceListener = null; XmlWriterTraceListener xmlTraceListenerWithCallStack = null; TraceSource mySource = null; try { mySource = new TraceSource("TestSource") { Switch = new SourceSwitch("switch1", "All") }; const TraceOptions baseTraceOutputOptions = TraceOptions.DateTime | TraceOptions.LogicalOperationStack | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Timestamp; xmlTraceListener = new XmlWriterTraceListener(@"C:\inetpub\wwwroot\des\dupage\Logs\Output.svclog") { TraceOutputOptions = baseTraceOutputOptions, Filter = new EventTypeFilter(SourceLevels.All) }; xmlTraceListenerWithCallStack = new XmlWriterTraceListener(@"C:\inetpub\wwwroot\des\dupage\Logs\OutputWithCallStack.svclog") { TraceOutputOptions = baseTraceOutputOptions | TraceOptions.Callstack, Filter = new EventTypeFilter(SourceLevels.Error) }; mySource.Listeners.Add(xmlTraceListener); mySource.Listeners.Add(xmlTraceListenerWithCallStack); const string testString = "<Test><InnerElement Val=\"1\" /><InnerElement Val=\"Data\"/><AnotherElement>11</AnotherElement></Test>"; var unEscapedXmlDiagnosticData = new UnescapedXmlDiagnosticData(testString); mySource.TraceEvent(TraceEventType.Start, 0, "Main Entry"); mySource.TraceInformation("Hello World!"); mySource.TraceEvent(TraceEventType.Error, 5, "Hello World!"); mySource.TraceData(TraceEventType.Critical, 11, 1, 2, 3); mySource.TraceData(TraceEventType.Information, 11, 11, 214, 2.3, "Hello", 't', unEscapedXmlDiagnosticData); Guid prevActivityId = Trace.CorrelationManager.ActivityId; Guid newActivityId = Guid.NewGuid(); Trace.CorrelationManager.ActivityId = newActivityId; mySource.TraceEvent(TraceEventType.Start, 0, "alpha"); Trace.CorrelationManager.StartLogicalOperation("WorkerThread"); mySource.Listeners[0].Filter = new SourceFilter("No match"); mySource.TraceData(TraceEventType.Error, 5, "SourceFilter should reject this message for the trace listener."); mySource.TraceEvent(TraceEventType.Stop, 0, "zulu"); Trace.CorrelationManager.ActivityId = prevActivityId; mySource.TraceTransfer(0, "transfer", newActivityId); Trace.CorrelationManager.StartLogicalOperation("AlphaThread"); mySource.Listeners[0].Filter = new SourceFilter("TestSource"); mySource.TraceData(TraceEventType.Error, 6, "SourceFilter should let this message through on the trace listener."); Trace.CorrelationManager.StopLogicalOperation(); mySource.Listeners[0].Filter = null; Trace.CorrelationManager.StopLogicalOperation(); mySource.TraceEvent(TraceEventType.Stop, 0, "Main Exit"); mySource.Flush(); mySource.Listeners.Clear(); mySource.Close(); xmlTraceListener = null; xmlTraceListenerWithCallStack = null; } finally { mySource = null; xmlTraceListener?.Dispose(); xmlTraceListenerWithCallStack?.Dispose(); } }