Esempio n. 1
0
        public void Write()
        {
            var cache    = new TraceEventCache();
            var listener = new FileLogTraceListener();

            listener.Location       = LogFileLocation.Custom;
            listener.CustomLocation = GetTestFilePath();

            listener.Write("Write");
            listener.WriteLine("WriteLine");
            listener.TraceEvent(eventCache: cache, source: "Source", eventType: TraceEventType.Warning, id: 3, message: "TraceEvent");
            listener.TraceData(eventCache: cache, source: "Source", eventType: TraceEventType.Error, id: 4, data: "TraceData");
            listener.Flush();
            listener.Close();
        }
        public void WriteTest()
        {
            using (FileLogTraceListener log = new FileLogTraceListener ()) {
                log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache ();

                log.TraceData (cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, null);
                log.Close ();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText (filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename);

                Assert.AreEqual ("nunit\tCritical\t0\t\r\n", data, "#01");
            }

            using (FileLogTraceListener log = new FileLogTraceListener ()) {
                log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache ();

                log.TraceData (cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "data");
                log.Close ();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText (filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename);

                Assert.AreEqual ("nunit\tCritical\t0\tdata\r\n", data, "#02");
            }

            using (FileLogTraceListener log = new FileLogTraceListener ()) {
                log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache ();

                log.TraceData (cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "data", "data2");
                log.Close ();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText (filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename);

                Assert.AreEqual ("nunit\tCritical\t0\tdata\tdata2\r\n", data, "#03");
            }

            using (FileLogTraceListener log = new FileLogTraceListener ()) {
                log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache ();

                log.TraceEvent (cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "msg");
                log.Close ();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText (filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename);

                Assert.AreEqual ("nunit\tCritical\t0\tmsg\r\n", data, "#04");
            }

            using (FileLogTraceListener log = new FileLogTraceListener ()) {
                log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache ();

                log.TraceEvent (cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "msg:{0}", "arg1");
                log.Close ();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText (filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename);

                Assert.AreEqual ("nunit\tCritical\t0\tmsg:arg1\r\n", data, "#05");
            }

            using (FileLogTraceListener log = new FileLogTraceListener ()) {
                log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache ();

                log.TraceOutputOptions = System.Diagnostics.TraceOptions.DateTime | System.Diagnostics.TraceOptions.LogicalOperationStack | System.Diagnostics.TraceOptions.ProcessId | System.Diagnostics.TraceOptions.ThreadId | System.Diagnostics.TraceOptions.Timestamp;
                log.TraceData (cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, null);
                log.Close ();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText (filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename);

                Assert.AreEqual ("nunit\tCritical\t0\t\t\"\"\t" + cache.DateTime.ToString ("u", CultureInfo.InvariantCulture) + "\t" + cache.ProcessId + "\t" + cache.ThreadId + "\t" + cache.Timestamp + System.Environment.NewLine, data, "#06");
            }

            using (FileLogTraceListener log = new FileLogTraceListener ()) {
                log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All);
                log.IncludeHostName = true;

                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache ();

                log.TraceOutputOptions = System.Diagnostics.TraceOptions.DateTime | System.Diagnostics.TraceOptions.LogicalOperationStack | System.Diagnostics.TraceOptions.ProcessId | System.Diagnostics.TraceOptions.ThreadId | System.Diagnostics.TraceOptions.Timestamp;
                log.TraceData (cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, null);
                log.Close ();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText (filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename);

                Assert.AreEqual ("nunit\tCritical\t0\t\t\"\"\t" + cache.DateTime.ToString ("u", CultureInfo.InvariantCulture) + "\t" + cache.ProcessId + "\t" + cache.ThreadId + "\t" + cache.Timestamp + "\t" + Environment.MachineName + System.Environment.NewLine, data, "#07");
            }
        }
        public void WriteTest()
        {
            using (FileLogTraceListener log = new FileLogTraceListener()) {
                log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache();

                log.TraceData(cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, null);
                log.Close();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename);

                Assert.AreEqual("nunit\tCritical\t0\t\r\n", data, "#01");
            }


            using (FileLogTraceListener log = new FileLogTraceListener()) {
                log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache();

                log.TraceData(cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "data");
                log.Close();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename);

                Assert.AreEqual("nunit\tCritical\t0\tdata\r\n", data, "#02");
            }

            using (FileLogTraceListener log = new FileLogTraceListener()) {
                log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache();

                log.TraceData(cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "data", "data2");
                log.Close();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename);

                Assert.AreEqual("nunit\tCritical\t0\tdata\tdata2\r\n", data, "#03");
            }

            using (FileLogTraceListener log = new FileLogTraceListener()) {
                log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache();

                log.TraceEvent(cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "msg");
                log.Close();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename);

                Assert.AreEqual("nunit\tCritical\t0\tmsg\r\n", data, "#04");
            }


            using (FileLogTraceListener log = new FileLogTraceListener()) {
                log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache();

                log.TraceEvent(cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, "msg:{0}", "arg1");
                log.Close();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename);

                Assert.AreEqual("nunit\tCritical\t0\tmsg:arg1\r\n", data, "#05");
            }

            using (FileLogTraceListener log = new FileLogTraceListener()) {
                log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All);
                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache();

                log.TraceOutputOptions = System.Diagnostics.TraceOptions.DateTime | System.Diagnostics.TraceOptions.LogicalOperationStack | System.Diagnostics.TraceOptions.ProcessId | System.Diagnostics.TraceOptions.ThreadId | System.Diagnostics.TraceOptions.Timestamp;
                log.TraceData(cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, null);
                log.Close();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename);

                Assert.AreEqual("nunit\tCritical\t0\t\t\"\"\t" + cache.DateTime.ToString("u", CultureInfo.InvariantCulture) + "\t" + cache.ProcessId + "\t" + cache.ThreadId + "\t" + cache.Timestamp + System.Environment.NewLine, data, "#06");
            }


            using (FileLogTraceListener log = new FileLogTraceListener()) {
                log.Filter          = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All);
                log.IncludeHostName = true;

                string filename = log.FullLogFileName;
                string data;
                System.Diagnostics.TraceEventCache cache = new System.Diagnostics.TraceEventCache();

                log.TraceOutputOptions = System.Diagnostics.TraceOptions.DateTime | System.Diagnostics.TraceOptions.LogicalOperationStack | System.Diagnostics.TraceOptions.ProcessId | System.Diagnostics.TraceOptions.ThreadId | System.Diagnostics.TraceOptions.Timestamp;
                log.TraceData(cache, "nunit", System.Diagnostics.TraceEventType.Critical, 0, null);
                log.Close();
                data = Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText(filename);
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename);

                Assert.AreEqual("nunit\tCritical\t0\t\t\"\"\t" + cache.DateTime.ToString("u", CultureInfo.InvariantCulture) + "\t" + cache.ProcessId + "\t" + cache.ThreadId + "\t" + cache.Timestamp + "\t" + Environment.MachineName + System.Environment.NewLine, data, "#07");
            }
        }