PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Echo/Program.cs

https://bitbucket.org/emertechie/csvlogtailer
C# | 88 lines | 70 code | 12 blank | 6 comment | 2 complexity | 43909f61cc145ccbe4daf0c7f4d54df4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. using CsvLogTailing;
  8. using Mono.Options;
  9. namespace Echo
  10. {
  11. /// <summary>
  12. /// This is a little test program that will echo tailed logs to console and optionally to
  13. /// file also (means you can do a quick file diff to make sure you are picking up all logs)
  14. /// </summary>
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. string logFileOrDirPath = null;
  20. string directoryFilter = null;
  21. string excludeRegexString = null;
  22. string[] columns = null;
  23. bool echoToFile = false;
  24. var optionSet = new OptionSet
  25. {
  26. { "p|path=", p => logFileOrDirPath = p },
  27. { "f|filter=", f => directoryFilter = f },
  28. { "x|excludeRegex=", x => excludeRegexString = x },
  29. { "c|columns=", c => columns = c.Split(',').Select(x => x.Trim()).ToArray() },
  30. { "ef|echotofile", p => echoToFile = true }
  31. };
  32. optionSet.Parse(args);
  33. Console.WriteLine("Press return to finish");
  34. var tailer = new CsvLogTailer(forceMemoryCollectionThreshold: 1000);
  35. var exceptions = new List<Exception>();
  36. var exSub = tailer.Exceptions.Subscribe(ex =>
  37. {
  38. Console.WriteLine("PARSE EXCEPTION");
  39. exceptions.Add(ex);
  40. });
  41. int i = 0;
  42. tailer
  43. .Tail(new CsvLogTailerSettings
  44. {
  45. FileOrDirectoryPath = logFileOrDirPath,
  46. DirectoryFilter = directoryFilter,
  47. FileNameExcludeRegex = String.IsNullOrWhiteSpace(excludeRegexString) ? null : new Regex(excludeRegexString, RegexOptions.IgnoreCase | RegexOptions.Compiled),
  48. ColumnNamesProvider = file => columns,
  49. BookmarkRepositoryUpdateFrequency = TimeSpan.FromSeconds(2)
  50. })
  51. .Subscribe(
  52. log =>
  53. {
  54. var fileName = Path.GetFileName(log.FilePath);
  55. string logLine = String.Join("|", log.LogFields);
  56. if (echoToFile)
  57. {
  58. var dir = Path.GetDirectoryName(log.FilePath);
  59. var echoFilePath = Path.Combine(dir, fileName + ".echo");
  60. File.AppendAllText(echoFilePath, logLine + Environment.NewLine);
  61. }
  62. //if (i++ % 100 == 0)
  63. // Console.WriteLine("[{0} {1}]: {2}", log.LogDateTime, fileName, logLine);
  64. },
  65. error => Console.WriteLine("ERROR (SUBSCRIPTION STOPPED): " + error.ToString()));
  66. Console.ReadLine();
  67. exSub.Dispose();
  68. if (exceptions.Any())
  69. {
  70. foreach (Exception exception in exceptions)
  71. Console.WriteLine(exception);
  72. Console.WriteLine("Showing {0} exceptions", exceptions.Count);
  73. }
  74. }
  75. }
  76. }