public void ParseStream()
 {
     using (var s = new MemoryStream())
     using (var w = new StreamWriter(s))
     {
         w.Write(_buffer);
         w.Flush();
         s.Position = 0;
         var entry = new LogEntryParser().Parse(s).Single();
         Assert.Equal("ERROR", entry.Data.Level.Name);
         Assert.Equal(@"AWESOMEMACHINE", entry.HostName);
         Assert.Equal(@"IsolatedAppDomainHost: IntegrationTests", entry.Data.Domain);
         Assert.Equal("msg", entry.Data.Message);
         Assert.Equal("IntegrationTests.LogTests", entry.Class());
         Assert.Equal("TestLog", entry.Method());
         Assert.Equal("19", entry.Line());
         Assert.Equal(@"C:\projects\LogViewer\IntegrationTests\LogTests.cs", entry.File());
     }
 }
Пример #2
0
        public void Parse3()
        {
            using (var s = new MemoryStream())
            using (var w = new StreamWriter(s))
            {

                var line =
                  @"<log4j:event
            logger=""IntegrationTests.LogTests""
            timestamp=""1300909721869""
            level=""ERROR"" thread=""7"">
            <log4j:message>msg</log4j:message>
            <log4j:properties>
            <log4j:data name=""log4net:UserName"" value=""AWESOMEMACHINE\Administrator"" />
            <log4j:data name=""log4jmachinename"" value=""AWESOMEMACHINE"" />
            <log4j:data name=""log4japp"" value=""IsolatedAppDomainHost: IntegrationTests"" />
            <log4j:data name=""log4net:HostName"" value=""AWESOMEMACHINE"" />
            </log4j:properties>
            <log4j:throwable>System.Exception: test</log4j:throwable>
            <log4j:locationInfo
            class=""IntegrationTests.LogTests"" method=""TestLog""
            file=""C:\projects\LogViewer\IntegrationTests\LogTests.cs""
            line=""27"" /></log4j:event>";

                w.Write(line);
                w.Flush();
                s.Position = 0;

                var entry = new LogEntryParser().Parse(s).Single();
                Assert.Equal("ERROR", entry.Data.Level.Name);
                Assert.Equal(@"AWESOMEMACHINE\Administrator", entry.Data.UserName);
                Assert.Equal(@"AWESOMEMACHINE", entry.MachineName);
                Assert.Equal(@"AWESOMEMACHINE", entry.HostName);
                Assert.Equal(@"IsolatedAppDomainHost: IntegrationTests", entry.Data.Domain);
                Assert.Equal("msg", entry.Data.Message);
                Assert.Equal("IntegrationTests.LogTests", entry.Class());
                Assert.Equal("System.Exception: test", entry.Data.ExceptionString);
                Assert.Equal("TestLog", entry.Method());
                Assert.Equal("27", entry.Line());
                Assert.Equal(@"C:\projects\LogViewer\IntegrationTests\LogTests.cs", entry.File());
            }
        }
        public void ParseStreamAtPosition()
        {
            long p = 0;
            var path = Path.GetTempFileName();
            using (var s = new FileStream(path,
                FileMode.Truncate,
                FileAccess.ReadWrite))
            using (var w = new StreamWriter(s))
            {
                w.Write(_buffer);
                w.Flush();
                s.Position = 0;
                var entry = new LogEntryParser().Parse(s).Single();// read written entry
                p = s.Position;
            }
            using (var s = new FileStream(path,
                FileMode.Append,
                FileAccess.Write))
            using (var w = new StreamWriter(s))
            {
                w.Write(_buffer);
                w.Flush();
            }

            using (var s = FileUtil.OpenReadOnly(path, position: p))
            {
                var entry = new LogEntryParser().Parse(s).Single();
                Assert.Equal("ERROR", entry.Data.Level.Name);
                Assert.Equal(@"AWESOMEMACHINE", entry.HostName);
                Assert.Equal(@"IsolatedAppDomainHost: IntegrationTests", entry.Data.Domain);
                Assert.Equal("msg", entry.Data.Message);
                Assert.Equal("IntegrationTests.LogTests", entry.Class());
                Assert.Equal("TestLog", entry.Method());
                Assert.Equal("19", entry.Line());
                Assert.Equal(@"C:\projects\LogViewer\IntegrationTests\LogTests.cs", entry.File());
            }
        }