public void Parse()
        {
            using (var s = new MemoryStream())
            using (var w = new StreamWriter(s))
            {
                var lines = @"<log4j:event logger=""HomeController"" timestamp=""1361528145331"" level=""ERROR"" thread=""91"">
            <log4j:message>error</log4j:message>
            <log4j:properties>
            <log4j:data name=""log4net:UserName"" value=""SOMEDOMAIN\someuser"" />
            <log4j:data name=""log4net:Identity"" value=""44045"" />
            <log4j:data name=""log4jmachinename"" value=""somemachine"" />
            <log4j:data name=""log4japp"" value=""/LM/W3SVC/1"" />
            <log4j:data name=""log4net:HostName"" value=""SOMEHOST"" />
            </log4j:properties>
            <log4j:locationInfo class=""MyController"" method=""Log"" file=""C:\project\MyController.cs"" line=""99"" />
            </log4j:event>
            ";

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

                var entry = new LogEntryParser().Parse(s).Single();

                Assert.Equal("44045", entry.Identity);
            }
        }
        public void Parse2()
        {
            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("System.Exception: test", entry.Data.ExceptionString);
            }
        }
 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.That(entry.Data.Level.Name, Is.EqualTo("ERROR"));
         Assert.That(entry.HostName, Is.EqualTo(@"AWESOMEMACHINE"));
         Assert.That(entry.Data.Domain, Is.EqualTo(@"IsolatedAppDomainHost: IntegrationTests"));
         Assert.That(entry.Data.Message, Is.EqualTo("msg"));
         Assert.That(entry.Class, Is.EqualTo("IntegrationTests.LogTests"));
         Assert.That(entry.Method, Is.EqualTo("TestLog"));
         Assert.That(entry.Line, Is.EqualTo("19"));
         Assert.That(entry.File, Is.EqualTo(@"C:\projects\LogViewer\IntegrationTests\LogTests.cs"));
     }
 }
示例#4
0
 public LogFileReaderBase(FileWithPosition file, LogEntryParser parser = null, IInvoker invoker = null)
 {
     this.File = file;
     this.parser = parser ?? new LogEntryParser();
     this.invoker = invoker ?? new DirectInvoker();
 }
示例#5
0
 public Watcher(FileWithPosition file, LogEntryParser parser = null, IInvoker invoker = null)
     : base(file,parser,invoker)
 {
 }
示例#6
0
 public Poller(FileWithPosition file, long duration, LogEntryParser parser = null, IInvoker invoker = null)
     : base(file,parser,invoker)
 {
     this.duration = duration;
 }
        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.That(entry.Data.Level.Name, Is.EqualTo("ERROR"));
                Assert.That(entry.HostName, Is.EqualTo(@"AWESOMEMACHINE"));
                Assert.That(entry.Data.Domain, Is.EqualTo(@"IsolatedAppDomainHost: IntegrationTests"));
                Assert.That(entry.Data.Message, Is.EqualTo("msg"));
                Assert.That(entry.Class, Is.EqualTo("IntegrationTests.LogTests"));
                Assert.That(entry.Method, Is.EqualTo("TestLog"));
                Assert.That(entry.Line, Is.EqualTo("19"));
                Assert.That(entry.File, Is.EqualTo(@"C:\projects\LogViewer\IntegrationTests\LogTests.cs"));
            }
        }
 public void ParseStreamAtPositionShouldThrowException()
 {
     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;
     }
     Assert.Throws<OutOfBoundsException>(() =>
     {
         using (var s = FileUtil.OpenReadOnly(path, position: p * 10))
         {
             new LogEntryParser().Parse(s);
         }
     });
 }
        public void Parse_with_text_element()
        {
            var line = @"<log4j:event logger=""installrelease"" timestamp=""1388750967872"" level=""ERROR"" thread=""""><log4j:NDC></log4j:NDC><log4j:message>Caught Errno::EACCES: Permission denied - filesomething
              C:/Ruby193/lib/ruby/gems/1.9.1/gems/someliblib/lib/libloblob/libloblob.rb:42:in `block in download'
              C:/Ruby193/lib/ruby/gems/1.9.1/gems/someliblib/lib/libloblob/libloblob.rb:41:in `each'
              C:/Ruby193/lib/ruby/gems/1.9.1/gems/someliblib/lib/libloblob/libloblob.rb:41:in `download'
              install_release.rb:92:in `block in get_latest!'</log4j:message><log4j:throwable>Caught Errno::EACCES: Permission denied - file
              C:/Ruby193/lib/ruby/gems/1.9.1/gems/someliblib/lib/libloblob/libloblob.rb:42:in `block in download'
              C:/Ruby193/lib/ruby/gems/1.9.1/gems/someliblib/lib/libloblob/libloblob.rb:41:in `each'
              C:/Ruby193/lib/ruby/gems/1.9.1/gems/someliblib/lib/libloblob/libloblob.rb:41:in `download'
              install_release.rb:92:in `block in get_latest!'</log4j:throwable><log4j:locationInfo class="""" method="""" file="""" line=""""/><log4j:properties></log4j:properties></log4j:event>";
            using (var s = new MemoryStream())
            using (var w = new StreamWriter(s))
            {
                w.Write(line);
                w.Flush();
                s.Position = 0;

                var entry = new LogEntryParser().Parse(s).Single();
                Assert.Equal("ERROR", entry.Data.Level.Name);
                Assert.True(entry.Data.Message.Contains(@"Caught Errno::EACCES: Permission denied - filesomething"));
                Assert.True(entry.Data.Message.Contains(@"install_release.rb:92:in `block in get_latest!'"));
            }
        }
        public void ParseFaulty()
        {
            using (var s = FileUtil.OpenReadOnly("test.xml"))
            {
                var entry = new LogEntryParser().Parse(s).Single();
                Assert.True(entry.Data.Message.Contains("Translation for 'Detta ska jag g"), "translation");
                Assert.True(entry.Data.Message.Contains("ra...' in module Todo for culture sv-SE does not exist."), "in module");

            }
        }
        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\Administrator", entry.Data.UserName);
                Assert.Equal(@"AWESOMEMACHINE", entry.MachineName);
                Assert.Equal(@"AWESOMEMACHINE", entry.HostName);
                Assert.Equal(@"IsolatedAppDomainHost: IntegrationTests", entry.Data.Domain);
                Assert.Equal("test", entry.Data.Message);
                Assert.Equal("IntegrationTests.LogTests", entry.Class());
                Assert.Equal("TestLog", entry.Method());
                Assert.Equal("18", entry.Line());
                Assert.Equal(@"C:\projects\LogViewer\IntegrationTests\LogTests.cs", entry.File());
            }
        }
示例#12
0
 public IEnumerable<LogEntry> Read(LogEntryParser parser)
 {
     using (var file = FileUtil.OpenReadOnly(FileName, position))
     {
         foreach (var item in parser.Parse(file))
         {
             yield return item;
         }
         position = file.Position;
     }
 }
示例#13
0
 private void constructPaginator()
 {
     LogEntryParser log = new LogEntryParser();
     FileStream stream = new FileStream(debugFileDB, FileMode.Open);
     IEnumerable<LogViewer.LogEntry> loge = log.Parse(stream);
     stream.Close();
     debugData = new List<LogViewer.LogEntry>(loge);
     debugData.Reverse();
     this.totalRows = debugData.Count;
     this.totalPages = (this.totalRows + this.pageSize - 1) / this.pageSize;
 }
示例#14
0
        static void Main(string[] args)
        {
            var files = new List<string>();
            int monitor = 0;
            int? lines = null;
            var watch = false;
            PatternLayout layout=null;
            var help = false;
            var p = new OptionSet() {
                { "f|file=",   v => { files.Add(v); } },
                { "m|monitor=", v => { monitor=Int32.Parse(v);}},
                { "w|watch", v => { watch = true;}},
                { "l|lines=", v => { lines=Int32.Parse(v);}},
                { "h|?|help", v => { help = true;}},
                { "y|layout=",v=> { layout=new PatternLayout(v);}}
            };
            var detectedFiles = args
                .Where(a=>!(a.StartsWith("-") || a.StartsWith("/")))
                .Where(a=> Uri.IsWellFormedUriString(a, UriKind.RelativeOrAbsolute))
                .Where(a=> File.Exists(a));
            // does not seem to work. add tests
            files.AddRange(detectedFiles);

            p.Parse(args);
            Action<LogEntry> showentry;
            if (null != layout)
            {
                showentry = l => layout.Format(Console.Out, new LoggingEvent(l.Data));
            }
            else
            {
                showentry = l => Console.WriteLine(l.Data.Message);
            }
            if (help)
            {
                Console.WriteLine(@"Usage:
            -f|file={a filename}
            The file to watch, monitor or

            -l|lines={tail x lines}
            Display the last x lines. Defaults to 10 lines.

            -y|layout={pattern layout syntax as defined in log4net.Layout.PatternLayout}

            -h|?|help
            Display help

            For instance to :
            LogTail.exe -f=logfile.xml
            LogTail.exe -file=logfile.xml
            ");
                return;
            }

            if (watch)
            {
                Do(new Watcher(new FileWithPosition(files.Single()))
                {
                    logentry = showentry
                });
                return;
            }
            if (monitor > 0)
            {
                Do(new Poller(new FileWithPosition(files.Single()), monitor)
                {
                    logentry = showentry
                });
                return;
            }

            if (files.Any()){
                TailFiles(lines ?? 10, files, showentry);
                return;
            }
            else
            {
                Console.WriteLine("No files, listening to standard input.");
                using (Stream stdin = Console.OpenStandardInput())
                using (Stream stdout = Console.OpenStandardOutput())
                using (StreamWriter writer = new  StreamWriter(stdout))
                {
                    var items = new LogEntryParser().Parse(stdin).ToArray();
                    foreach (var logEntry in items.Skip(items.Count() - (lines??10)))
                    {
                        writer.WriteLine(logEntry.Data.Message);
                    }
                }
                return;
            }
        }
示例#15
0
 private static void TailFiles(int lines, List<string> files, Action<LogEntry> showentry)
 {
     foreach (var fileName in files)
     {
         using (var file = FileUtil.OpenReadOnly(fileName))
         {
             var items = new LogEntryParser().Parse(file)
                 .ToArray();
             foreach (var logEntry in items.Skip(items.Count() - lines))
             {
                 showentry(logEntry);
             }
         }
     }
 }
示例#16
0
        static void Main(string[] args)
        {
            var files = new List<string>();
            int monitor = 0;
            int? lines = null;
            var watch = false;
            LayoutSkeleton layout = null;
            var help = false;
            var p = new OptionSet() {
                { "i|file=",   v => { files.Add(v); } },
                { "m|monitor=", v => { monitor=ParseInterval(v);}},
                { "w|watch", v => { watch = true;}},
                { "l|lines=", v => { lines=Int32.Parse(v);}},
                { "h|?|help", v => { help = true;}},
                { "y|layout=",v=> { layout=new PatternLayout(v);}},
                { "f|format=", v=> { layout = GetFormatLayout(v); }}
            };
            var detectedFiles = args
                .Where(a => !(a.StartsWith("-") || a.StartsWith("/")))
                .Where(a => File.Exists(a));
            files.AddRange(detectedFiles);

            p.Parse(args);
            if (layout == null)
            {
                layout = new SimpleLayout();
            }
            Action<TextWriter,LogEntry> showentry = (writer, l) => layout.Format(writer, new LoggingEvent(l.Data));

            if (help)
            {
                Console.WriteLine(@"Usage:
            -i|file={a filename}
            The (input) file to watch, monitor or

            -l|lines={tail x lines}
            Display the last x lines. Defaults to 10 lines.

            -y|layout={pattern layout syntax as defined in log4net.Layout.PatternLayout}

            -f|format={a named layout format}
            The available formats are:
            -f=minusminus Information delimited by newline and ----------------------

            -h|?|help
            Display help

            -w|watch
            Use file system watcher to watch file

            -m|monitor=seconds
            Use polling to check for changes of the file.

            For instance to :
            LogTail.exe -f=logfile.xml
            LogTail.exe -file=logfile.xml

            If you are in powershell (or cygwin) you can do the following:
            cat yourlogfile.xml | LogTail.exe
            ");
                return;
            }

            if (watch)
            {
                var w = new Watcher<LogEntry>(new FileWithPosition(files.Single()), new LogEntryParser());
                w.LogEntry += (entry) => showentry(Console.Out, entry);

                Do(w);
                return;
            }
            if (monitor > 0)
            {
                var w = new Poller<LogEntry>(new FileWithPosition(files.Single()), monitor, new LogEntryParser());
                w.LogEntry += (entry) => showentry(Console.Out, entry);
                Do(w);
                return;
            }

            if (files.Any())
            {
                TailFiles(lines ?? 10, files, (entry) => showentry(Console.Out, entry));
                return;
            }
            else
            {
                Console.WriteLine("No files, listening to standard input.");
                using (Stream stdin = Console.OpenStandardInput())
                using (Stream stdout = Console.OpenStandardOutput())
                using (StreamWriter writer = new StreamWriter(stdout))
                {
                    var items = new LogEntryParser().Parse(stdin).ToArray();
                    foreach (var logEntry in items.Skip(items.Count() - (lines ?? 10)))
                    {
                        showentry(writer, logEntry);
                    }
                }
                return;
            }
        }