public void FileRollOverTest() { var mockFileSystem = new MockFileSystem(); var listener = new RollingFileTraceListener("Log{DateTime:HHmmss}"); listener.FileSystem = mockFileSystem; listener.TraceEvent(null, "souce", TraceEventType.Information, 1, "A"); Thread.Sleep(TimeSpan.FromSeconds(2)); listener.TraceEvent(null, "souce", TraceEventType.Information, 2, "B"); listener.Flush(); Assert.AreEqual(2, mockFileSystem.OpenedItems.Count); }
public static void InitializeLogging() { var consoleListener = new ColoredConsoleTraceListener(); Trace.Listeners.Add(consoleListener); Trace.AutoFlush = true; if (App.Config["filelog"].ToBoolean(true) == true) { var fileListener = new RollingFileTraceListener(); //fileListener.Attributes[""] = ""; Trace.Listeners.Add(fileListener); } }
public void FileHandlesEventSentDirectly() { var mockFileSystem = new MockFileSystem(); var listener = new RollingFileTraceListener(null); listener.FileSystem = mockFileSystem; listener.TraceEvent(null, "source", TraceEventType.Information, 1, "{0}-{1}", 2, "A"); listener.Flush(); Assert.AreEqual(1, mockFileSystem.OpenedItems.Count); var tuple0 = mockFileSystem.OpenedItems[0]; // (earlier name was "QTAgent32-") // VS2012 process name "vstest.executionengine.x86-" // VS2015 process name "te.processhost.managed-" //StringAssert.StartsWith(tuple0.Item1, "vstest.executionengine.x86-" + DateTimeOffset.Now.Year.ToString()); var data = tuple0.Item2.GetBuffer(); var output = Encoding.UTF8.GetString(data, 0, (int)tuple0.Item2.Length); StringAssert.Contains(output, "Information source 1: 2-A"); }
private static void InitializeTracing(ManagedJob job, LogElement logElement) { if (logElement == null) { logElement = new LogElement(); logElement.FileName = job.Name + ".log"; logElement.LogLevel = InstanceConfig.CurrentInstance.Log.LogLevel; logElement.MaxLogSize = InstanceConfig.CurrentInstance.Log.MaxLogSize; } // Check if path includes a directory part of some sort if (!logElement.FileName.Contains(Path.DirectorySeparatorChar.ToString())) { // Append server directory logElement.FileName = Path.Combine(Path.GetDirectoryName(InstanceConfig.CurrentInstance.Log.FileName), logElement.FileName); } job.Tracing.Switch.Level = ConfigurationHelper.ConvertLogLevel(logElement.LogLevel); RollingFileTraceListener listener = new RollingFileTraceListener(logElement.FileName, logElement.MaxLogSize); //listener.TraceOutputOptions = TraceOptions.DateTime |TraceOptions.ThreadId; // todo fixme job.Tracing.Listeners.Add(listener); }
public void DisposedTextWriterShouldNotBeReused() { var template = "textWriterTest{0}.log"; var a1 = Guid.NewGuid(); var a2 = Guid.NewGuid(); var a1File = string.Format(template, a1); var a2Files = Enumerable.Repeat(string.Format(template, a2), 1) .Concat( Enumerable.Range(1, 5) .Select(i => string.Format(template, string.Format("{0}-{1}", a2, i))) ) .ToArray(); try { //lock files for a2 using (var f0 = File.Open(a2Files[0], FileMode.Append, FileAccess.Write, FileShare.Read)) using (var f1 = File.Open(a2Files[1], FileMode.Append, FileAccess.Write, FileShare.Read)) using (var f2 = File.Open(a2Files[2], FileMode.Append, FileAccess.Write, FileShare.Read)) using (var f3 = File.Open(a2Files[3], FileMode.Append, FileAccess.Write, FileShare.Read)) using (var f4 = File.Open(a2Files[4], FileMode.Append, FileAccess.Write, FileShare.Read)) using (var f5 = File.Open(a2Files[5], FileMode.Append, FileAccess.Write, FileShare.Read)) using (var listener = new RollingFileTraceListener(string.Format(template, "{ACTIVITYID}"))) { Trace.CorrelationManager.ActivityId = a1; listener.TraceEvent( null, "source", TraceEventType.Information, 1, "A1.1"); try { Trace.CorrelationManager.ActivityId = a2; listener.TraceEvent( null, "source", TraceEventType.Information, 1, "A2.1"); } catch (InvalidOperationException) { //should be exception about exosted number of retries //we swallow it } //now trace with the first TextWriter and it should not throw "System.ObjectDisposedException : Cannot write to a closed TextWriter" expection Trace.CorrelationManager.ActivityId = a1; listener.TraceEvent( null, "source", TraceEventType.Information, 1, "A1.2"); } } finally { //clean up files if (File.Exists(a1File)) { File.Delete(a1File); } foreach (var f in a2Files) { if (File.Exists(f)) { File.Delete(f); } } } }