Example #1
0
        public static void DisplayProperties(TraceSource ts)
        {
            Console.WriteLine("TraceSource name = " + ts.Name);
            Console.WriteLine("TraceSource switch level = " + ts.Switch.Level);
            Console.WriteLine("TraceSource switch = " + ts.Switch.DisplayName);
            SwitchAttribute[] switches = SwitchAttribute.GetAll(Assembly.GetExecutingAssembly());
            for (int i = 0; i < switches.Length; i++)
            {
                Console.WriteLine("Switch name = " + switches[i].SwitchName);
                Console.WriteLine("Switch Type = " + switches[i].SwitchType);
            }

            // Get the custom attributes for the TraceSource.
            Console.WriteLine("Number of custom trace source attributes = "
                              + ts.Attributes.Count);
            foreach (DictionaryEntry de in ts.Attributes)
            {
                Console.WriteLine("Custom trace source attribute = "
                                  + de.Key + "  " + de.Value);
            }
            // Get the custom attributes for the trace source switch.
            foreach (DictionaryEntry de in ts.Switch.Attributes)
            {
                Console.WriteLine("Custom switch attribute = "
                                  + de.Key + "  " + de.Value);
            }
            Console.WriteLine("Number of listeners = " + ts.Listeners.Count);
            foreach (TraceListener traceListener in ts.Listeners)
            {
                Console.Write("TraceListener: " + traceListener.Name + "\t");
                // The following output can be used to update the configuration file.
                Console.WriteLine("AssemblyQualifiedName = " +
                                  (traceListener.GetType().AssemblyQualifiedName));
            }
        }
Example #2
0
        //</Snippet2>
        static void Main()
        {
            try
            {
                // Initialize trace switches.
                //<Snippet3>
#if (!ConfigFile)
                TraceSwitch traceSwitch = new TraceSwitch("TraceSwitch", "Verbose");
#endif
                //</Snippet3>
                //<Snippet4>
                Console.WriteLine(traceSwitch.Level);
                //</Snippet4>
                //<Snippet5>
#if (!ConfigFile)
                BooleanSwitch boolSwitch = new BooleanSwitch("BoolSwitch", "True");
#endif
                //</Snippet5>
                //<Snippet6>
                Console.WriteLine(boolSwitch.Enabled);
                //</Snippet6>
                //<Snippet7>
#if (!ConfigFile)
                SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose");
#endif
                //</Snippet7>
                //<Snippet8>
                Console.WriteLine(sourceSwitch.Level);
                //</Snippet8>
                // Initialize trace source.
                //<Snippet9>
                MyTraceSource ts = new MyTraceSource("TraceTest");
                //</Snippet9>
                //<Snippet10>
                Console.WriteLine(ts.Switch.DisplayName);
                //</Snippet10>
                //<Snippet11>
                Console.WriteLine(ts.Switch.Level);
                //</Snippet11>
                //<Snippet12>
                SwitchAttribute[] switches = SwitchAttribute.GetAll(typeof(TraceTest).Assembly);
                for (int i = 0; i < switches.Length; i++)
                {
                    Console.Write(switches[i].SwitchName);
                    Console.Write("\t");
                    Console.WriteLine(switches[i].SwitchType);
                }
                //</Snippet12>
                //<Snippet13>
                // Display the SwitchLevelAttribute for the BooleanSwitch.
                Object[] attribs = typeof(BooleanSwitch).GetCustomAttributes(typeof(SwitchLevelAttribute), false);
                if (attribs.Length == 0)
                {
                    Console.WriteLine("Error, couldn't find SwitchLevelAttribute on BooleanSwitch.");
                }
                else
                {
                    Console.WriteLine(((SwitchLevelAttribute)attribs[0]).SwitchLevelType.ToString());
                }
                //</Snippet13>
#if (ConfigFile)
                //<Snippet14>
                // Get the custom attributes for the TraceSource.
                Console.WriteLine(ts.Attributes.Count);
                foreach (DictionaryEntry de in ts.Attributes)
                {
                    Console.WriteLine(de.Key + "  " + de.Value);
                }
                //</Snippet14>
                //<Snippet15>
                // Get the custom attributes for the trace source switch.
                foreach (DictionaryEntry de in ts.Switch.Attributes)
                {
                    Console.WriteLine(de.Key + "  " + de.Value);
                }
                //</Snippet15>
#endif
                //<Snippet16>
                ts.Listeners["console"].TraceOutputOptions |= TraceOptions.Callstack;
                //</Snippet16>
                //<Snippet17>
                ts.TraceEvent(TraceEventType.Warning, 1);
                //</Snippet17>
                ts.Listeners["console"].TraceOutputOptions = TraceOptions.DateTime;
                //<Snippet18>
                // Issue file not found message as a warning.
                ts.TraceEvent(TraceEventType.Warning, 1, "File Test not found");
                //</Snippet18>
                System.Threading.Thread.Sleep(1000);
                //<Snippet19>
                Console.WriteLine(ts.Listeners.Count);
                //</Snippet19>
                //<Snippet20>
                ts.Listeners.Add(new TestListener(TESTLISTENERFILE, "TestListener"));
                //</Snippet20>
                Console.WriteLine(ts.Listeners.Count);
                //<Snippet22>
                foreach (TraceListener traceListener in ts.Listeners)
                {
                    Console.Write(traceListener.Name + "\t");
                    // The following output can be used to update the configuration file.
                    Console.WriteLine((traceListener.GetType().AssemblyQualifiedName));
                }
                //</Snippet22>
                //<Snippet23>
                // Show correlation and output options.
                Trace.CorrelationManager.StartLogicalOperation("abc");
                //</Snippet23>
                //<Snippet24>
                // Issue file not found message as a verbose event using a formatted string.
                ts.TraceEvent(TraceEventType.Verbose, 2, "File {0} not found.", "test");
                //</Snippet24>
                Trace.CorrelationManager.StartLogicalOperation("def");
                // Issue file not found message as information.
                ts.TraceEvent(TraceEventType.Information, 3, "File {0} not found.", "test");
                //<Snippet25>
                Trace.CorrelationManager.StopLogicalOperation();
                //</Snippet25>
                //<Snippet26>
                ts.Listeners["console"].TraceOutputOptions |= TraceOptions.LogicalOperationStack;
                //</Snippet26>
                // Issue file not found message as an error event.
                ts.TraceEvent(TraceEventType.Error, 4, "File {0} not found.", "test");
                Trace.CorrelationManager.StopLogicalOperation();
                // Write simple message from trace.
                //<Snippet27>
                Trace.TraceWarning("Warning from Trace.");
                //</Snippet27>
                //<Snippet28>
                // Test the filter on the ConsoleTraceListener.
                ts.Listeners["console"].Filter = new SourceFilter("No match");
                ts.TraceData(TraceEventType.Information, 5,
                             "SourceFilter should reject this message for the console trace listener.");
                ts.Listeners["console"].Filter = new SourceFilter("TraceTest");
                ts.TraceData(TraceEventType.Information, 6,
                             "SourceFilter should let this message through on the console trace listener.");
                //</Snippet28>
                ts.Listeners["console"].Filter = null;
                // Use the TraceData method.
                //<Snippet30>
                ts.TraceData(TraceEventType.Warning, 9, new object());
                //</Snippet30>
                //<Snippet31>
                ts.TraceData(TraceEventType.Warning, 10, new object[] { "Message 1", "Message 2" });
                //</Snippet31>
                // Activity tests.
                //<Snippet32>
                ts.TraceEvent(TraceEventType.Start, 11, "Will not appear until the switch is changed.");
                ts.Switch.Level = SourceLevels.ActivityTracing | SourceLevels.Critical;
                ts.TraceEvent(TraceEventType.Suspend, 12, "Switch includes ActivityTracing, this should appear");
                ts.TraceEvent(TraceEventType.Critical, 13, "Switch includes Critical, this should appear");
                //</Snippet32>

                Trace.Flush();
                ((TextWriterTraceListener)Trace.Listeners["file"]).Close();
                ((TextWriterTraceListener)Trace.Listeners["fileBuilder"]).Close();

                System.Threading.Thread.Sleep(1000);


                // Examine the contents of the log file.
                Console.WriteLine("\n\n\n");
                // Use try/catch in case the file hasn't been defined.
                try
                {
                    Console.WriteLine("Examining " + TEXTFILE);
                    StreamReader logfile = new StreamReader(TEXTFILE);
                    while (!logfile.EndOfStream)
                    {
                        Console.WriteLine(logfile.ReadLine());
                    }

                    logfile.Close();

                    File.Delete(TEXTFILE);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error tying to read the text log file. " + e.Message);
                }

                // Display the contents of the log file builder.
                Console.WriteLine("\n\n\n");
                // Use try/catch in case the file hasn't been defined.
                try
                {
                    Console.WriteLine("Examining " + DELIMITEDFILE);
                    StreamReader logfile = new StreamReader(DELIMITEDFILE);
                    while (!logfile.EndOfStream)
                    {
                        Console.WriteLine(logfile.ReadLine());
                    }
                    logfile.Close();
                    File.Delete(DELIMITEDFILE);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error tying to read the delimited text file. " + e.Message);
                }

                Console.WriteLine("Press any key to exit.");
                Console.Read();
            }
            catch (Exception e)
            {
                // Catch any unexpected exception.
                Console.WriteLine("Unexpected exception: " + e.ToString());
                Console.Read();
            }
        }