public static void LogProcessor___Valid___Works() { var tempFileName = Path.GetTempFileName(); try { // Arrange var configuration = new FileLogConfig(LogInclusionKindToOriginsMaps.ExceptionsFromAnywhere, tempFileName); var processor = new FileLogWriter(configuration); var infoCanary = A.Dummy <string>(); var errorCanary = new ArgumentException(A.Dummy <string>()); // Act processor.Log(infoCanary.ToLogEntry().ToLogItem(LogItemOrigin.ItsLogEntryPosted)); processor.Log(errorCanary.ToLogEntry().ToLogItem(LogItemOrigin.ItsLogEntryPosted)); // Assert var fileContents = File.ReadAllText(tempFileName); fileContents.Should().NotContain(infoCanary); fileContents.Should().Contain(errorCanary.Message); } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } }
public void Log_writes_xml_to_output_stream() { var stream = new MemoryStream(); var writer = new StreamWriter(stream); var fileLogWriter = new FileLogWriter((dir, file) => writer); const LogLevel level = LogLevel.Error; const String message = "Test"; fileLogWriter.Log(level, message); stream.Seek(0, SeekOrigin.Begin); var lDocument = new XmlDocument(); using (var xmlReader = XmlReader.Create(stream, new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment })) { lDocument.Load(xmlReader); } var lRootElement = lDocument.FirstChild as XmlElement; Assert.IsNotNull(lRootElement); Assert.AreEqual(level.ToString().ToLower(), lRootElement.Name); Assert.IsTrue(lRootElement.HasAttribute(Resources.TimestampAttributeName)); // Check timestamp attribute contains a parseable datetime representation. // The actual value isn't important and will change each time the test is run anyway. DateTime datetime; Assert.IsTrue(DateTime.TryParse(lRootElement.GetAttribute(Resources.TimestampAttributeName), out datetime)); Assert.IsTrue(lRootElement.HasAttribute(Resources.MessageAttributeName)); Assert.AreEqual(message, lRootElement.GetAttribute(Resources.MessageAttributeName)); }
public void Log_flushes_output_stream() { var writer = Substitute.For <TextWriter>(); var fileLogWriter = new FileLogWriter((dir, file) => writer); fileLogWriter.Log(LogLevel.Error, null); writer.Received().Flush(); }
public void Log_flushes_output_stream() { var writer = Substitute.For<TextWriter>(); var fileLogWriter = new FileLogWriter((dir, file) => writer); fileLogWriter.Log(LogLevel.Error, null); writer.Received().Flush(); }
public void Log_silently_does_nothing_if_stream_is_null() { var fileLogWriter = new FileLogWriter((dir, file) => null); fileLogWriter.Log(LogLevel.Error, null); }