2 // TraceListenerTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using NUnit.Framework;
36 using System.Diagnostics;
37 using System.Threading;
39 namespace MonoTests.System.Diagnostics
42 public class TraceListenerTest
45 public void GetSupportedAttributes ()
47 MyTraceListener t = new MyTraceListener ();
48 string [] attributes = t.SupportedAttributes;
49 Assert.IsNull (attributes);
53 public void TraceEventAndTraceData ()
55 StringWriter sw = new StringWriter ();
56 TextWriterTraceListener t = new TextWriterTraceListener (sw);
57 t.TraceEvent (null, null, TraceEventType.Error, 0, null);
58 t.TraceEvent (null, "bulldog", TraceEventType.Error, 0);
59 TraceEventCache cc = new TraceEventCache ();
60 t.TraceData (cc, null, TraceEventType.Error, 0);
61 t.TraceData (cc, null, TraceEventType.Error, 0);
62 t.TraceTransfer (null, "bulldog", 0, "hoge", Guid.Empty);
64 string expected = @" Error: 0 :
68 bulldog Transfer: 0 : hoge, relatedActivityId=00000000-0000-0000-0000-000000000000
70 Assert.AreEqual (expected.Replace ("\r\n", "\n"), sw.ToString ().Replace ("\r\n", "\n"));
74 public void TraceOptionValues ()
76 Assert.AreEqual (TraceOptions.None,
77 new TextWriterTraceListener (TextWriter.Null).TraceOutputOptions, "#1");
78 Assert.AreEqual (TraceOptions.None,
79 new XmlWriterTraceListener (TextWriter.Null).TraceOutputOptions, "#2");
81 StringWriter sw = new StringWriter ();
82 TextWriterTraceListener t = new TextWriterTraceListener (sw);
83 Trace.CorrelationManager.StartLogicalOperation ("foo");
84 Trace.CorrelationManager.StartLogicalOperation ("bar");
85 t.TraceOutputOptions = TraceOptions.LogicalOperationStack | TraceOptions.DateTime | TraceOptions.Timestamp;// | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Callstack;
86 t.TraceEvent (null, null, TraceEventType.Error, 0, null);
87 t.TraceEvent (null, "bulldog", TraceEventType.Error, 0);
88 TraceEventCache cc = new TraceEventCache ();
89 DateTime date = cc.DateTime;
90 long time = cc.Timestamp;
91 t.TraceData (cc, null, TraceEventType.Error, 0);
92 t.TraceData (cc, null, TraceEventType.Error, 0);
93 t.TraceTransfer (null, "bulldog", 0, "hoge", Guid.Empty);
94 Trace.CorrelationManager.StopLogicalOperation ();
95 Trace.CorrelationManager.StopLogicalOperation ();
97 string expected = String.Format (@" Error: 0 :
100 LogicalOperationStack=bar, foo
104 Callstack= at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
105 at System.Environment.get_StackTrace()
106 at System.Diagnostics.TraceEventCache.get_Callstack()
107 at System.Diagnostics.TraceListener.WriteFooter(TraceEventCache eventCache)
108 at System.Diagnostics.TraceListener.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object[] data)
109 at MonoTests.System.Diagnostics.TraceListenerTest.TraceOptionValues()
114 LogicalOperationStack=bar, foo
117 bulldog Transfer: 0 : hoge, relatedActivityId=00000000-0000-0000-0000-000000000000
118 ", date.ToString ("o"), time); // date and time are in current culture
119 Assert.AreEqual (expected.Replace ("\r\n", "\n"), sw.ToString ().Replace ("\r\n", "\n"));
122 class MyTraceListener : TraceListener
124 public string [] SupportedAttributes {
125 get { return base.GetSupportedAttributes (); }
128 public override void Write (string message)
132 public override void WriteLine (string message)