public void FileIsRolledWhenIntervalSetToMidnightAndOverwrite() { fileNameWithoutExtension = Guid.NewGuid().ToString(); fileName = fileNameWithoutExtension + Extension; using (RollingFlatFileTraceListener traceListener = new RollingFlatFileTraceListener(fileName, "--header--", "--footer--", null, 0, String.Empty, RollFileExistsBehavior.Overwrite, RollInterval.Midnight)) { traceListener.TraceData(new TraceEventCache(), "source", TraceEventType.Information, 0, "logged message 1"); traceListener.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.SetCurrentDateTime(DateTime.Now); Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary()); dateTimeProvider.SetCurrentDateTime(DateTime.Now.AddDays(1).Date); //dateTimeProvider.currentDateTime = DateTime.Now.AddMinutes(2); Assert.IsTrue(traceListener.RollingHelper.UpdateRollingInformationIfNecessary()); traceListener.TraceData(new TraceEventCache(), "source", TraceEventType.Information, 1, "logged message 2"); } Assert.IsTrue(File.Exists(fileName)); Assert.IsTrue(LogFileReader.ReadFileWithoutLock(fileName).Contains("logged message 2")); Assert.IsFalse(LogFileReader.ReadFileWithoutLock(fileName).Contains("logged message 1")); string[] archiveFiles = Directory.GetFiles(".", fileNameWithoutExtension + "*"); Assert.AreEqual(1, archiveFiles.Length); }
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) { if (!ShouldLog(eventCache, source, eventType, id, null, null, data, null)) { return; } Initialise(); _rollingListener.TraceData(eventCache, source, eventType, id, data); _rollingListener.Flush(); }
public void RollingFlatFileTraceListenerReplacedEnviromentVariablesWillFallBackIfNotPrivilegesToRead() { string environmentVariable = "%USERPROFILE%"; string fileName = Path.Combine(environmentVariable, "foo.log"); EnvironmentPermission denyPermission = new EnvironmentPermission(PermissionState.Unrestricted); denyPermission.Deny(); try { RollingFlatFileTraceListener listener = new RollingFlatFileTraceListener(fileName, "header", "footer", null, 1, "", RollFileExistsBehavior.Increment, RollInterval.Day); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 1, "This is a test"); listener.Dispose(); } catch (InvalidOperationException) { throw; } finally { EnvironmentPermission.RevertAll(); } Assert.Fail("Permission was not denied."); }
public void RollingFlatFileTraceListenerReplacedEnviromentVariables() { string fileName = @"%USERPROFILE%\foo.log"; string fileNameFromListener = string.Empty; RollingFlatFileTraceListener listener = new RollingFlatFileTraceListener(fileName, "header", "footer", null, 1, "", RollFileExistsBehavior.Increment, RollInterval.Day); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 1, "This is a test"); listener.Dispose(); string expandedFileName = Environment.ExpandEnvironmentVariables(fileName); bool result = File.Exists(expandedFileName); Assert.IsTrue(result); using (FileStream stream = File.Open(expandedFileName, FileMode.Open)) { fileNameFromListener = stream.Name; } File.Delete(expandedFileName); Assert.AreEqual(expandedFileName, fileNameFromListener); }
public void WillRollExistingFileIfOverDateThreshold() { string existingPayload = new string('c', 10); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (RollingFlatFileTraceListener traceListener = new RollingFlatFileTraceListener(fileName, "header", "footer", null, 1, "yyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day)) { traceListener.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = currentDateTime.AddDays(2); traceListener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 0, "logged message"); } Assert.AreEqual(existingPayload, File.ReadAllText(fileNameWithoutExtension + ".2007" + extension)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void WillTruncateExistingFileIfOverSizeThresholdAndNoPatternIsSpecifiedForOverwriteBehavior() { string existingPayload = new string('c', 5000); DateTime currentDateTime = new DateTime(2007, 1, 1); File.WriteAllText(fileName, existingPayload); File.SetCreationTime(fileName, currentDateTime); using (RollingFlatFileTraceListener traceListener = new RollingFlatFileTraceListener(fileName, "header", "footer", null, 1, "", RollFileExistsBehavior.Overwrite, RollInterval.None)) { traceListener.RollingHelper.DateTimeProvider = dateTimeProvider; dateTimeProvider.currentDateTime = currentDateTime; traceListener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 0, "logged message"); } Assert.IsFalse(File.ReadAllText(fileName).Contains(existingPayload)); Assert.IsTrue(File.ReadAllText(fileName).Contains("logged message")); }
public void DontWriteHeaderOrFooterWhenEventsAreFiltered() { const string header = "MockHeader"; const string footer = "MockFooter"; const string fileName = "rolling.log"; string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\" + fileName; try { LogEntry log = new LogEntry("Header nor footer written", "Category", 1, 1, TraceEventType.Error, "FilteredEventsDontWriteHeaderNorFooter", null); RollingFlatFileTraceListener listener = new RollingFlatFileTraceListener(filePath, header, footer, null, 100, "mmddyyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day); listener.Filter = new EventTypeFilter(SourceLevels.Off); listener.TraceData(null, "Error", TraceEventType.Error, 1, log); listener.Flush(); listener.Close(); Assert.IsTrue(File.Exists(filePath)); StreamReader reader = new StreamReader(filePath); string content = reader.ReadToEnd(); reader.Close(); Assert.IsFalse(content.Contains(header)); Assert.IsFalse(content.Contains(footer)); } finally { File.Delete(filePath); } }
public static void CheckListener() { string environmentVariable = "%USERPROFILE%"; string fileName = Path.Combine(environmentVariable, "test.log"); RollingFlatFileTraceListener listener = new RollingFlatFileTraceListener(fileName, "header", "footer", null, 1, "", RollFileExistsBehavior.Increment, RollInterval.Day); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 1, "This is a test"); listener.Dispose(); }
public void RollingFlatFileTraceListenerReplacedInexistingEnviromentVariables() { string fileName = @"tmp\%FOO%\%MY_VARIABLE%\foo.log"; RollingFlatFileTraceListener listener = new RollingFlatFileTraceListener(fileName, "header", "footer", null, 1, "", RollFileExistsBehavior.Increment, RollInterval.Day); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 1, "This is a test"); listener.Dispose(); string expandedFileName = EnvironmentHelper.ReplaceEnvironmentVariables(fileName); string expectedFileName = Path.GetFileName(expandedFileName); bool result = File.Exists(expandedFileName); Assert.IsTrue(result); File.Delete(expandedFileName); }
public void DontWriteHeaderOrFooterWhenEventsAreFiltered() { const string header = "MockHeader"; const string footer = "MockFooter"; const string fileName = "rolling.log"; string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\" + fileName; try { LogEntry log = new LogEntry("Header nor footer written", "Category", 1, 1, TraceEventType.Error, "FilteredEventsDontWriteHeaderNorFooter", null); RollingFlatFileTraceListener listener = new RollingFlatFileTraceListener(filePath, header, footer, null, 100, "mmddyyyy", RollFileExistsBehavior.Overwrite, RollInterval.Day); listener.Filter = new EventTypeFilter(SourceLevels.Off); listener.TraceData(null, "Error", TraceEventType.Error, 1, log); listener.Flush(); listener.Close(); Assert.IsTrue(File.Exists(filePath)); StreamReader reader = new StreamReader(filePath); string content = reader.ReadToEnd(); reader.Close(); Assert.IsFalse(content.Contains(header)); Assert.IsFalse(content.Contains(footer)); } finally { File.Delete(filePath); } }