public void WriteLine1 () { StringWriter sw = new StringWriter (); XmlWriterTraceListener x = new XmlWriterTraceListener (sw); x.WriteLine ("sample"); x.Close (); Assert.AreEqual (sample1.Replace ('\'', '"'), sw.ToString ()); }
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(); }