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 AppendTest() { using (FileLogTraceListener log = new FileLogTraceListener()) { log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All); log.Append = false; 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); 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); log.Append = true; 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); Assert.AreEqual("nunit\tCritical\t0\t\r\n" + "nunit\tCritical\t0\t\r\n", data, "#02"); } using (FileLogTraceListener log = new FileLogTraceListener()) { log.Filter = new System.Diagnostics.EventTypeFilter(System.Diagnostics.SourceLevels.All); log.Append = false; 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); Assert.AreEqual("nunit\tCritical\t0\t\r\n", data, "#03"); Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(filename); } }
public void AppendTest() { using (FileLogTraceListener log = new FileLogTraceListener ()) { log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All); log.Append = false; 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); 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); log.Append = true; 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); Assert.AreEqual ("nunit\tCritical\t0\t\r\n" + "nunit\tCritical\t0\t\r\n", data, "#02"); } using (FileLogTraceListener log = new FileLogTraceListener ()) { log.Filter = new System.Diagnostics.EventTypeFilter (System.Diagnostics.SourceLevels.All); log.Append = false; 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); Assert.AreEqual ("nunit\tCritical\t0\t\r\n", data, "#03"); Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile (filename); } }
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"); } }
/// <summary> /// 关闭并释放日志监听器 /// </summary> public static void CloseLogListener() { Info("关闭日志记录器 ..."); LogListener?.Close(); LogListener?.Dispose(); }
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"); } }