public StructuredTextLogger(string path, IAnalytics analytics) { _depth = 0; _path = path; _analytics = analytics; _structuredTextDocument = new StructuredTextDocument<LogEntry>(path, // DateTime.ToString("o") => "2015-08-04T00:08:38.5489308Z" e => string.Join(LogEntrySeparator, e.LogTime.ToString("o"), e.Message, e.Id, (int)e.Type), str => { var splitted = str.Split(new[] { LogEntrySeparator }, StringSplitOptions.None); if (splitted.Length == 4) { var time = DateTime.Parse(splitted[0]).ToUniversalTime(); var message = UnsanitizeValue(splitted[1]); var id = splitted[2]; var type = (LogEntryType)Int32.Parse(splitted[3]); return new LogEntry(time, id, message, type); } else { throw new FormatException(string.Format("the log line \"{0}\" is in an invalid format", str)); } }); }
public void StructuredDocumentLogsWithCorrectDepth(string logEntry, int depth) { //Arrange var fileSystem = new Mock <IFileSystem>(); var fileBase = new Mock <FileBase>(); var stream = new TestMemoryStream(); var structuredDocument = new StructuredTextDocument <string>(@"x:\log.log", Id, Id); fileSystem.Setup(f => f.File).Returns(fileBase.Object); fileBase.Setup(f => f.Open(It.IsAny <string>(), It.IsAny <FileMode>(), It.IsAny <FileAccess>(), It.IsAny <FileShare>())) .Returns(stream); FileSystemHelpers.Instance = fileSystem.Object; //Act structuredDocument.Write(logEntry, depth); //Assert Assert.True(stream.Position > 0, "must write data"); stream.Position = 0; var result = new StreamReader(stream).ReadToEnd(); var writtenDepth = StructuredTextDocument.GetEntryDepth(result); Assert.Equal(depth, writtenDepth); Assert.Equal(string.Concat(logEntry, System.Environment.NewLine), result.Substring(writtenDepth)); stream.RealDispose(); }
public void StructuredDocumentParsingTest() { //Arrange FileSystemHelpers.Instance = MockFileSystem.GetMockFileSystem(@"x:\log.log", () => "Message 1\r\nmessage 2\r\nmessage 3\r\n\tsub message 3.1\r\n\tsub message 3.2\r\n\tsub message 3.3\r\nmessage 4\r\n\tsub message 4.1"); var ExpectedContent = new int[] { 0, 0, 3, 1 }; var structuredDocument = new StructuredTextDocument <string>(@"x:\log.log", Id, Id); //Act var parsed = structuredDocument.GetDocument().ToArray(); //Assert Assert.Equal(4, parsed.Length); for (var i = 0; i < parsed.Count(); i++) { Assert.Equal(ExpectedContent[i], parsed[i].Children.Count()); } }
public void StructuredDocumentThrowsAFormatExceptionForInvalidLogEntries(string logEntry) { //Arrange Exception exception = null; var structuredDocument = new StructuredTextDocument <string>(@"x:\deployment\log.log", Id, Id); //Act try { structuredDocument.Write(logEntry, 0); } catch (Exception e) { exception = e; } //Assert Assert.IsType(typeof(FormatException), exception); Assert.Contains("Serialized log of type", exception.Message); }