From 4422fd0509c8b5608eda9ef8a8e31029b0a6f7fd Mon Sep 17 00:00:00 2001 From: Jonathan Preddy Date: Wed, 23 Jul 2014 11:16:56 -0400 Subject: [PATCH] Further refactoring of inputs. --- TimberWinR.UnitTests/Configuration.cs | 39 ++--- TimberWinR/Configuration.cs | 1 - TimberWinR/Filters/DateFilter.cs | 15 -- TimberWinR/Filters/FilterBase.cs | 19 ++- TimberWinR/Filters/GrokFilter.cs | 16 +- TimberWinR/Inputs/IISLog.cs | 176 +------------------- TimberWinR/Inputs/IISW3CLog.cs | 223 ++------------------------ TimberWinR/Inputs/InputBase.cs | 56 ++++++- TimberWinR/Inputs/TailFileInput.cs | 20 +-- TimberWinR/Inputs/WindowsEvent.cs | 27 +--- 10 files changed, 106 insertions(+), 486 deletions(-) diff --git a/TimberWinR.UnitTests/Configuration.cs b/TimberWinR.UnitTests/Configuration.cs index 1eb1a31..9b18ca7 100644 --- a/TimberWinR.UnitTests/Configuration.cs +++ b/TimberWinR.UnitTests/Configuration.cs @@ -17,49 +17,42 @@ namespace TimberWinR.UnitTests public void OutputEvents() { - foreach (var evt in c.Events.ToArray()) - { + foreach (var evt in c.Events) Console.WriteLine(evt); - } } public void OutputLogs() { - foreach (var log in c.Logs.ToArray()) - { + foreach (var log in c.Logs) Console.WriteLine(log); - } } public void OutputIIS() { - foreach (var iis in c.IIS.ToArray()) - { + foreach (var iis in c.IIS) Console.WriteLine(iis); - } } public void OutputIISW3C() { - foreach (var iisw3c in c.IISW3C.ToArray()) - { + foreach (var iisw3c in c.IISW3C) Console.WriteLine(iisw3c); - } } - public void OutputGroks() + public void OutputFilters() { - - //IEnumerable filters = c.Filters; - - //foreach (var grok in c.Filters) - // Console.WriteLine(grok); + foreach (var filter in c.Filters) + Console.WriteLine(filter); } [Test] - public void Test1() + public void Output() { - Assert.AreEqual(c.Logs.ToArray()[1].Name, "Second Set"); + OutputEvents(); + OutputLogs(); + OutputIIS(); + OutputIISW3C(); + OutputFilters(); } [Test] @@ -226,7 +219,6 @@ namespace TimberWinR.UnitTests bool fullEventCode = false; string direction = "FW"; string stringsSep = "|"; - string iCheckpoint; string binaryFormat = "PRINT"; TimberWinR.Inputs.WindowsEvent evt = c.Events.ToArray()[0]; @@ -251,7 +243,6 @@ namespace TimberWinR.UnitTests int iCodepage = 0; int recurse = 0; bool splitLongLines = false; - string iCheckpoint; TimberWinR.Inputs.TailFileInput log = c.Logs.ToArray()[0]; @@ -299,11 +290,9 @@ namespace TimberWinR.UnitTests string location = @"c:\inetpub\logs\LogFiles\W3SVC1\*"; int iCodepage = -2; int recurse = 0; - string minDateMod; bool dQuotes = false; bool dirTime = false; bool consolidateLogs = false; - string iCheckpoint; TimberWinR.Inputs.IISW3CLog iisw3c = c.IISW3C.ToArray()[0]; @@ -315,7 +304,7 @@ namespace TimberWinR.UnitTests Assert.AreEqual(dQuotes, iisw3c.DQuotes); Assert.AreEqual(dirTime, iisw3c.DirTime); Assert.AreEqual(consolidateLogs, iisw3c.ConsolidateLogs); - Assert.IsNull(iisw3c.ICheckpoint); + Assert.IsEmpty(iisw3c.ICheckpoint); } } } diff --git a/TimberWinR/Configuration.cs b/TimberWinR/Configuration.cs index 0967081..c3bee3e 100644 --- a/TimberWinR/Configuration.cs +++ b/TimberWinR/Configuration.cs @@ -180,7 +180,6 @@ namespace TimberWinR break; default: throw new Exception(string.Format("Unknown tag: {0}", e.Name.ToString())); - break; } } } diff --git a/TimberWinR/Filters/DateFilter.cs b/TimberWinR/Filters/DateFilter.cs index f23cfcb..ab92698 100644 --- a/TimberWinR/Filters/DateFilter.cs +++ b/TimberWinR/Filters/DateFilter.cs @@ -48,21 +48,6 @@ namespace TimberWinR.Filters } } - public override string ToString() - { - StringBuilder sb = new StringBuilder(); - sb.Append("DateFilter\n"); - foreach (var prop in this.GetType().GetProperties()) - { - if (prop != null) - { - sb.Append(String.Format("\t{0}: {1}\n", prop.Name, prop.GetValue(this, null))); - } - - } - return sb.ToString(); - } - public override void Apply(JObject json) { JToken token = null; diff --git a/TimberWinR/Filters/FilterBase.cs b/TimberWinR/Filters/FilterBase.cs index 46162f1..0b36282 100644 --- a/TimberWinR/Filters/FilterBase.cs +++ b/TimberWinR/Filters/FilterBase.cs @@ -41,6 +41,23 @@ namespace TimberWinR.Filters } } return retValue; - } + } + + public override string ToString() + { + StringBuilder sb = new StringBuilder(); + + sb.Append(String.Format("{0}\n", this.GetType().ToString())); + foreach (var prop in this.GetType().GetProperties()) + { + if (prop != null) + { + sb.Append(String.Format("\t{0}: {1}\n", prop.Name, prop.GetValue(this, null))); + } + + } + return sb.ToString(); + } + } } diff --git a/TimberWinR/Filters/GrokFilter.cs b/TimberWinR/Filters/GrokFilter.cs index 0c6c9c0..2759757 100644 --- a/TimberWinR/Filters/GrokFilter.cs +++ b/TimberWinR/Filters/GrokFilter.cs @@ -35,6 +35,8 @@ namespace TimberWinR.Filters AddFields = new List(); RemoveFields = new List(); + DropIfMatch = ParseBoolAttribute(parent, "dropIfMatch", false); + ParseMatch(parent); ParseAddFields(parent); ParseDropIfMatch(parent); @@ -134,20 +136,6 @@ namespace TimberWinR.Filters } } - public override string ToString() - { - StringBuilder sb = new StringBuilder(); - sb.Append("GrokFilter\n"); - foreach (var prop in this.GetType().GetProperties()) - { - if (prop != null) - { - sb.Append(String.Format("\t{0}: {1}\n", prop.Name, prop.GetValue(this, null))); - } - } - return sb.ToString(); - } - public override void Apply(Newtonsoft.Json.Linq.JObject json) { JToken token = null; diff --git a/TimberWinR/Inputs/IISLog.cs b/TimberWinR/Inputs/IISLog.cs index 02a1963..11b0698 100644 --- a/TimberWinR/Inputs/IISLog.cs +++ b/TimberWinR/Inputs/IISLog.cs @@ -10,14 +10,12 @@ namespace TimberWinR.Inputs { public string Name { get; private set; } public string Location { get; private set; } - public List Fields { get; private set; } - - // Parameters public int ICodepage { get; private set; } public int Recurse { get; private set; } public string MinDateMod { get; private set; } public string Locale { get; private set; } public string ICheckpoint { get; private set; } + public List Fields { get; private set; } public static void Parse(List iislogs, XElement iislogElement) { @@ -31,154 +29,16 @@ namespace TimberWinR.Inputs public IISLog(XElement parent) { - ParseName(parent); - ParseLocation(parent); - - // Default values for parameters. - ICodepage = -2; - Recurse = 0; - Locale = "DEF"; - - ParseICodepage(parent); - ParseRecurse(parent); - ParseMinDateMod(parent); - ParseLocale(parent); - ParseICheckpoint(parent); - + Name = ParseRequiredStringAttribute(parent, "name"); + Location = ParseRequiredStringAttribute(parent, "location"); + ICodepage = ParseIntAttribute(parent, "iCodepage", -2); + Recurse = ParseIntAttribute(parent, "recurse", 0); + MinDateMod = ParseDateAttribute(parent, "minDateMod"); + Locale = ParseStringAttribute(parent, "locale", "DEF"); + ICheckpoint = ParseStringAttribute(parent, "iCheckpoint"); ParseFields(parent); } - private void ParseName(XElement parent) - { - string attributeName = "name"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - Name = a.Value; - } - catch - { - throw new TimberWinR.ConfigurationErrors.MissingRequiredAttributeException(parent, attributeName); - } - } - - private void ParseLocation(XElement parent) - { - string attributeName = "location"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - Location = a.Value; - } - catch - { - throw new TimberWinR.ConfigurationErrors.MissingRequiredAttributeException(parent, attributeName); - } - } - - private void ParseICodepage(XElement parent) - { - string attributeName = "iCodepage"; - int valInt; - - try - { - XAttribute a = parent.Attribute(attributeName); - - if (int.TryParse(a.Value, out valInt)) - { - ICodepage = valInt; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeIntegerValueException(a); - } - } - catch - { - } - } - - private void ParseRecurse(XElement parent) - { - string attributeName = "recurse"; - int valInt; - - try - { - XAttribute a = parent.Attribute(attributeName); - - if (int.TryParse(a.Value, out valInt)) - { - Recurse = valInt; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeIntegerValueException(a); - } - } - catch - { - } - } - - private void ParseMinDateMod(XElement parent) - { - string attributeName = "minDateMod"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - DateTime dt; - if (DateTime.TryParseExact(a.Value, - "yyyy-MM-dd hh:mm:ss", - CultureInfo.InvariantCulture, - DateTimeStyles.None, - out dt)) - { - MinDateMod = a.Value; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeDateValueException(a); - } - - Locale = a.Value; - } - catch { } - } - - private void ParseLocale(XElement parent) - { - string attributeName = "locale"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - Locale = a.Value; - } - catch { } - } - - private void ParseICheckpoint(XElement parent) - { - string attributeName = "iCheckpoint"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - ICheckpoint = a.Value; - } - catch { } - } - private void ParseFields(XElement parent) { Dictionary allPossibleFields = new Dictionary() @@ -205,25 +65,5 @@ namespace TimberWinR.Inputs Fields = base.parseFields(parent, allPossibleFields); } - public override string ToString() - { - StringBuilder sb = new StringBuilder(); - sb.Append("IISLog\n"); - sb.Append(String.Format("Name: {0}\n", Name)); - sb.Append(String.Format("Location: {0}\n", Location)); - sb.Append("Fields:\n"); - foreach (FieldDefinition f in Fields) - { - sb.Append(String.Format("\t{0}\n", f.Name)); - } - sb.Append("Parameters:\n"); - sb.Append(String.Format("\tiCodepage: {0}\n", ICodepage)); - sb.Append(String.Format("\trecurse: {0}\n", Recurse)); - sb.Append(String.Format("\tminDateMod: {0}\n", MinDateMod)); - sb.Append(String.Format("\tlocale: {0}\n", Locale)); - sb.Append(String.Format("\tiCheckpoint: {0}\n", ICheckpoint)); - - return sb.ToString(); - } } } diff --git a/TimberWinR/Inputs/IISW3CLog.cs b/TimberWinR/Inputs/IISW3CLog.cs index ff31d49..fd07863 100644 --- a/TimberWinR/Inputs/IISW3CLog.cs +++ b/TimberWinR/Inputs/IISW3CLog.cs @@ -9,9 +9,6 @@ namespace TimberWinR.Inputs { public string Name { get; private set; } public string Location { get; private set; } - public List Fields { get; private set; } - - // Parameters public int ICodepage { get; private set; } public int Recurse { get; private set; } public string MinDateMod { get; private set; } @@ -19,6 +16,7 @@ namespace TimberWinR.Inputs public bool DirTime { get; private set; } public bool ConsolidateLogs { get; private set; } public string ICheckpoint { get; private set; } + public List Fields { get; private set; } public static void Parse(List iisw3clogs, XElement iisw3clogElement) { @@ -32,198 +30,17 @@ namespace TimberWinR.Inputs public IISW3CLog(XElement parent) { - ParseName(parent); - ParseLocation(parent); - - // Default values for parameters. - ICodepage = -2; - Recurse = 0; - DQuotes = false; - DirTime = false; - ConsolidateLogs = false; - - ParseICodepage(parent); - ParseRecurse(parent); - ParseDQuotes(parent); - ParseDirTime(parent); - ParseConsolidateLogs(parent); - ParseICheckpoint(parent); - + Name = ParseRequiredStringAttribute(parent, "name"); + Location = ParseRequiredStringAttribute(parent, "location"); + ICodepage = ParseIntAttribute(parent, "iCodepage", -2); + Recurse = ParseIntAttribute(parent, "recurse", 0); + DQuotes = ParseBoolAttribute(parent, "dQuotes", false); + DirTime = ParseBoolAttribute(parent, "dirTime", false); + ConsolidateLogs = ParseBoolAttribute(parent, "consolidateLogs", false); + ICheckpoint = ParseStringAttribute(parent, "iCheckpoint"); ParseFields(parent); } - private void ParseName(XElement parent) - { - string attributeName = "name"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - Name = a.Value; - } - catch - { - throw new TimberWinR.ConfigurationErrors.MissingRequiredAttributeException(parent, attributeName); - } - } - - private void ParseLocation(XElement parent) - { - string attributeName = "location"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - Location = a.Value; - } - catch - { - throw new TimberWinR.ConfigurationErrors.MissingRequiredAttributeException(parent, attributeName); - } - } - - private void ParseICodepage(XElement parent) - { - string attributeName = "iCodepage"; - int valInt; - - try - { - XAttribute a = parent.Attribute(attributeName); - - if (int.TryParse(a.Value, out valInt)) - { - ICodepage = valInt; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeIntegerValueException(a); - } - } - catch - { - } - } - - private void ParseRecurse(XElement parent) - { - string attributeName = "recurse"; - int valInt; - - try - { - XAttribute a = parent.Attribute(attributeName); - - if (int.TryParse(a.Value, out valInt)) - { - Recurse = valInt; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeIntegerValueException(a); - } - } - catch - { - } - } - - private void ParseDQuotes(XElement parent) - { - string attributeName = "dQuotes"; - string value; - - try - { - XAttribute a = parent.Attribute(attributeName); - - value = a.Value; - - if (value == "ON" || value == "true") - { - DQuotes = true; - } - else if (value == "OFF" || value == "false") - { - DQuotes = false; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(parent.Attribute(attributeName)); - } - } - catch { } - } - - private void ParseDirTime(XElement parent) - { - string attributeName = "dirTime"; - string value; - - try - { - XAttribute a = parent.Attribute(attributeName); - - value = a.Value; - - if (value == "ON" || value == "true") - { - DirTime = true; - } - else if (value == "OFF" || value == "false") - { - DirTime = false; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(parent.Attribute(attributeName)); - } - } - catch { } - } - - private void ParseConsolidateLogs(XElement parent) - { - string attributeName = "consolidateLogs"; - string value; - - try - { - XAttribute a = parent.Attribute(attributeName); - - value = a.Value; - - if (value == "ON" || value == "true") - { - ConsolidateLogs = true; - } - else if (value == "OFF" || value == "false") - { - ConsolidateLogs = false; - } - else - { - throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(parent.Attribute(attributeName)); - } - } - catch { } - } - - private void ParseICheckpoint(XElement parent) - { - string attributeName = "iCheckpoint"; - - try - { - XAttribute a = parent.Attribute(attributeName); - - ICheckpoint = a.Value; - } - catch { } - } - private void ParseFields(XElement parent) { Dictionary allPossibleFields = new Dictionary() @@ -265,27 +82,5 @@ namespace TimberWinR.Inputs Fields = base.parseFields(parent, allPossibleFields); } - public override string ToString() - { - StringBuilder sb = new StringBuilder(); - sb.Append("IISW3CLog\n"); - sb.Append(String.Format("Name: {0}\n", Name)); - sb.Append(String.Format("Location: {0}\n", Location)); - sb.Append("Fields:\n"); - foreach (FieldDefinition f in Fields) - { - sb.Append(String.Format("\t{0}\n", f.Name)); - } - sb.Append("Parameters:\n"); - sb.Append(String.Format("\tiCodepage: {0}\n", ICodepage)); - sb.Append(String.Format("\trecurse: {0}\n", Recurse)); - sb.Append(String.Format("\tminDateMod: {0}\n", MinDateMod)); - sb.Append(String.Format("\tdQuotes: {0}\n", DQuotes)); - sb.Append(String.Format("\tdirTime: {0}\n", DirTime)); - sb.Append(String.Format("\tconsolidateLogs: {0}\n", ConsolidateLogs)); - sb.Append(String.Format("\tiCheckpoint: {0}\n", ICheckpoint)); - - return sb.ToString(); - } } } diff --git a/TimberWinR/Inputs/InputBase.cs b/TimberWinR/Inputs/InputBase.cs index 3759ad8..622c227 100644 --- a/TimberWinR/Inputs/InputBase.cs +++ b/TimberWinR/Inputs/InputBase.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Data.Common; +using System.Globalization; using System.Linq; +using System.Text; using System.Xml.Linq; namespace TimberWinR.Inputs @@ -71,13 +73,36 @@ namespace TimberWinR.Inputs return retValue; } + protected static string ParseDateAttribute(XElement e, string attributeName, string defaultValue = "") + { + string retValue = defaultValue; + XAttribute a = e.Attribute(attributeName); + if (a != null) + { + DateTime dt; + if (DateTime.TryParseExact(a.Value, + "yyyy-MM-dd hh:mm:ss", + CultureInfo.InvariantCulture, + DateTimeStyles.None, + out dt)) + { + retValue = a.Value; + } + else + { + throw new TimberWinR.ConfigurationErrors.InvalidAttributeDateValueException(a); + } + } + + return retValue; + } + protected static bool ParseRequiredBoolAttribute(XElement e, string attributeName) { XAttribute a = e.Attribute(attributeName); if (a == null) throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); - bool retValue = false; switch (a.Value) { case "ON": @@ -87,7 +112,6 @@ namespace TimberWinR.Inputs case "OFF": case "false": return false; - break; default: throw new TimberWinR.ConfigurationErrors.InvalidAttributeValueException(e.Attribute(attributeName)); @@ -143,6 +167,32 @@ namespace TimberWinR.Inputs } } return retValue; - } + } + + public override string ToString() + { + StringBuilder sb = new StringBuilder(); + sb.Append(String.Format("{0}\n", this.GetType().ToString())); + sb.Append("Parameters:\n"); + foreach (var prop in this.GetType().GetProperties()) + { + if (prop != null) + { + if (prop.Name == "Fields") + { + sb.Append(String.Format("{0}:\n", prop.Name)); + foreach (FieldDefinition f in (List)prop.GetValue(this, null)) + { + sb.Append(String.Format("\t{0}\n", f.Name)); + } + } + else + { + sb.Append(String.Format("\t{0}: {1}\n", prop.Name, prop.GetValue(this, null))); + } + } + } + return sb.ToString(); + } } } diff --git a/TimberWinR/Inputs/TailFileInput.cs b/TimberWinR/Inputs/TailFileInput.cs index 700692e..35147aa 100644 --- a/TimberWinR/Inputs/TailFileInput.cs +++ b/TimberWinR/Inputs/TailFileInput.cs @@ -9,12 +9,10 @@ namespace TimberWinR.Inputs { public string Name { get; private set; } public string Location { get; private set; } - public List Fields { get; private set; } - - // Parameters public int ICodepage { get; private set; } public int Recurse { get; private set; } public bool SplitLongLines { get; private set; } + public List Fields { get; private set; } public static void Parse(List logs, XElement logElement) { @@ -48,21 +46,5 @@ namespace TimberWinR.Inputs Fields = base.parseFields(parent, allPossibleFields); } - public override string ToString() - { - StringBuilder sb = new StringBuilder(); - sb.Append("TextLog\n"); - sb.Append(String.Format("Name: {0}\n", Name)); - sb.Append(String.Format("Location: {0}\n", Location)); - sb.Append("Fields:\n"); - foreach (FieldDefinition f in Fields) - sb.Append(String.Format("\t{0}\n", f.Name)); - sb.Append("Parameters:\n"); - sb.Append(String.Format("\tiCodepage: {0}\n", ICodepage)); - sb.Append(String.Format("\trecurse: {0}\n", Recurse)); - sb.Append(String.Format("\tsplitLongLines: {0}\n", SplitLongLines)); - - return sb.ToString(); - } } } \ No newline at end of file diff --git a/TimberWinR/Inputs/WindowsEvent.cs b/TimberWinR/Inputs/WindowsEvent.cs index d0f0c6f..4a457c2 100644 --- a/TimberWinR/Inputs/WindowsEvent.cs +++ b/TimberWinR/Inputs/WindowsEvent.cs @@ -9,9 +9,6 @@ namespace TimberWinR.Inputs public class WindowsEvent : InputBase { public string Source { get; private set; } - public List Fields { get; private set; } - - // Parameters public bool FullText { get; private set; } public bool ResolveSIDS { get; private set; } public bool FormatMsg { get; private set; } @@ -21,6 +18,7 @@ namespace TimberWinR.Inputs public string StringsSep { get; private set; } public string ICheckpoint { get; private set; } public string BinaryFormat { get; private set; } + public List Fields { get; private set; } public static void Parse(List events, XElement eventElement) { @@ -70,28 +68,5 @@ namespace TimberWinR.Inputs Fields = base.parseFields(parent, allPossibleFields); } - public override string ToString() - { - StringBuilder sb = new StringBuilder(); - sb.Append("WindowsEvent\n"); - sb.Append(String.Format("Source: {0}\n", Source)); - sb.Append("Fields:\n"); - foreach (FieldDefinition f in Fields) - { - sb.Append(String.Format("\t{0}\n", f.Name)); - } - sb.Append("Parameters:\n"); - sb.Append(String.Format("\tfullText: {0}\n", FullText)); - sb.Append(String.Format("\tresolveSIDS: {0}\n", ResolveSIDS)); - sb.Append(String.Format("\tformatMsg: {0}\n", FormatMsg)); - sb.Append(String.Format("\tmsgErrorMode: {0}\n", MsgErrorMode)); - sb.Append(String.Format("\tfullEventCode: {0}\n", FullEventCode)); - sb.Append(String.Format("\tdirection: {0}\n", Direction)); - sb.Append(String.Format("\tstringsSep: {0}\n", StringsSep)); - sb.Append(String.Format("\tiCheckpoint: {0}\n", ICheckpoint)); - sb.Append(String.Format("\tbinaryFormat: {0}\n", BinaryFormat)); - - return sb.ToString(); - } } } \ No newline at end of file