Improved Error Handling
This commit is contained in:
@@ -47,6 +47,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
<None Include="testconf.xsd">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TimberWinR\TimberWinR.csproj">
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TimberWinR>
|
||||
|
||||
<Inputs>
|
||||
<WindowsEvents>
|
||||
<Events source="System,Application" binaryFormat="PRINT" />
|
||||
<Events source="System,Application" binaryFormat="PRINT" fullText="crap" />
|
||||
</WindowsEvents>
|
||||
<Logs>
|
||||
<Log name="First Set" location="C:\Logs1\*.log" />
|
||||
<Log name="First Set" location="C:\Logs1\*.log" />
|
||||
<Log name="Second Set" location="C:\Logs2\*.log" recurse="0" splitLongLines="OFF" />
|
||||
<Log name="Third Set" location="C:\Logs2\1.log,C:\Logs2\2.log" />
|
||||
</Logs>
|
||||
|
||||
<IISLogs>
|
||||
|
||||
|
||||
</IISLogs>
|
||||
</Inputs>
|
||||
|
||||
|
||||
190
TimberWinR.UnitTests/testconf.xsd
Normal file
190
TimberWinR.UnitTests/testconf.xsd
Normal file
@@ -0,0 +1,190 @@
|
||||
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<xs:element name="TimberWinR">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Inputs">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="WindowsEvents" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Events" maxOccurs="unbounded" minOccurs="0">
|
||||
<xs:complexType mixed="true">
|
||||
<xs:sequence>
|
||||
<xs:element name="Fields" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Field" maxOccurs="unbounded" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:string">
|
||||
<xs:attribute name="name" use="required">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="EventLog|RecordNumber|TimeGenerated|TimeWritten|EventID|EventType|EventTypeName|EventCategory|EventCategoryName|SourceName|Strings|ComputerName|SID|Message|Data" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:string" name="source" use="required"/>
|
||||
<xs:attribute name="fullText" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="ON|on|OFF|off|true|false" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="resolveSIDs" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="ON|on|OFF|off|true|false" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="formatMsg" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="ON|on|OFF|off|true|false" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="msgErrorMode" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="NULL|null|ERROR|error|MSG|msg" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="fullEventCode" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="ON|on|OFF|off|true|false" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="direction" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="FW|fw|BW|bw" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute type="xs:string" name="stringsSep" use="optional"/>
|
||||
<xs:attribute type="xs:string" name="iCheckpoint" use="optional"/>
|
||||
<xs:attribute name="binaryFormat" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="ASC|asc|PRINT|print|HEX|hex" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
<xs:element name="Logs" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Log" maxOccurs="unbounded" minOccurs="0">
|
||||
<xs:complexType mixed="true">
|
||||
<xs:sequence>
|
||||
<xs:element name="Fields" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Field" maxOccurs="unbounded" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:string">
|
||||
<xs:attribute name="name" use="required">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="LogFilename|Index|Text" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:string" name="name" use="required"/>
|
||||
<xs:attribute type="xs:string" name="location" use="required"/>
|
||||
<xs:attribute type="xs:int" name="iCodepage" use="optional"/>
|
||||
<xs:attribute type="xs:int" name="recurse" use="optional"/>
|
||||
<xs:attribute name="splitLongLines" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="ON|on|OFF|off|true|false" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute type="xs:string" name="iCheckpoint" use="optional"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
<xs:element name="IISLogs" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="IIS" maxOccurs="unbounded" minOccurs="0">
|
||||
<xs:complexType mixed="true">
|
||||
<xs:sequence>
|
||||
<xs:element name="Fields" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="Field" maxOccurs="unbounded" minOccurs="0">
|
||||
<xs:complexType>
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:string">
|
||||
<xs:attribute name="name" use="required">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:pattern value="LogFilename|LogRow|UserIP|UserName|Date|Time|ServiceInstance|HostName|ServerIP|TimeTaken|BytesSent|BytesReceived|StatusCode|Win32StatusCode|RequestType|Target|Parameters" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
<xs:attribute type="xs:string" name="name" use="required"/>
|
||||
<xs:attribute type="xs:string" name="location" use="required"/>
|
||||
<xs:attribute type="xs:int" name="iCodepage" use="optional"/>
|
||||
<xs:attribute type="xs:int" name="recurse" use="optional"/>
|
||||
<xs:attribute name="minDateMod" use="optional">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base="xs:token">
|
||||
<xs:pattern value="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" />
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute type="xs:string" name="locale" use="optional"/>
|
||||
<xs:attribute type="xs:string" name="iCheckpoint" use="optional"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -14,6 +14,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimberWinR.ServiceHost", "T
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimberWinR.UnitTests", "TimberWinR.UnitTests\TimberWinR.UnitTests.csproj", "{4410C95C-B22F-4B68-B132-84545AAA7A31}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1B2F600B-2400-45B9-A28E-CFC391D9EFA9}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
README.md = README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Odbc;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
@@ -10,6 +12,16 @@ namespace TimberWinR
|
||||
{
|
||||
public class Configuration
|
||||
{
|
||||
private class InvalidAttributeValueException : Exception
|
||||
{
|
||||
public InvalidAttributeValueException(XAttribute a, string badValue)
|
||||
: base(
|
||||
string.Format("{0}:{1} Invalid Attribute <{2} {3}=\"{4}\">", a.Document.BaseUri,
|
||||
((IXmlLineInfo)a).LineNumber, a.Parent.Name, a.Name, badValue))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private static List<WindowsEvents> _events = new List<WindowsEvents>();
|
||||
public IEnumerable<WindowsEvents> Events { get { return _events; } }
|
||||
|
||||
@@ -162,6 +174,7 @@ namespace TimberWinR
|
||||
foreach (XAttribute a in attributes)
|
||||
{
|
||||
string val = a.Value;
|
||||
IXmlLineInfo li = ((IXmlLineInfo)a);
|
||||
|
||||
switch (a.Name.ToString())
|
||||
{
|
||||
@@ -178,7 +191,7 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for fullText.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
case "resolveSIDS":
|
||||
@@ -192,7 +205,7 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for resolveSIDS.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
case "formatMsg":
|
||||
@@ -206,7 +219,7 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for formatMsg.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
case "msgErrorMode":
|
||||
@@ -216,7 +229,7 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for msgErrorMode.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
case "fullEventCode":
|
||||
@@ -230,7 +243,7 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for fullEventCode.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
case "direction":
|
||||
@@ -240,7 +253,7 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for direction.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
case "stringsSep":
|
||||
@@ -256,11 +269,11 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for binaryFormat.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine(String.Format("ERROR. WindowsEvents encountered unknown attribute: {0}.", a.Name.ToString()));
|
||||
throw new Exception(String.Format("ERROR. WindowsEvents encountered unknown attribute: {0}.", a.Name.ToString()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -314,7 +327,7 @@ namespace TimberWinR
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR. Unknown value declared for Logs:splitLongLines.");
|
||||
throw new InvalidAttributeValueException(a, val);
|
||||
}
|
||||
break;
|
||||
case "iCheckpoint":
|
||||
@@ -396,15 +409,13 @@ namespace TimberWinR
|
||||
|
||||
static void parseXMLConf(string xmlConfFile)
|
||||
{
|
||||
XDocument config = XDocument.Load(xmlConfFile);
|
||||
XDocument config = XDocument.Load(xmlConfFile, LoadOptions.SetLineInfo | LoadOptions.SetBaseUri);
|
||||
|
||||
IEnumerable<XElement> inputs =
|
||||
from el in config.Root.Descendants("Inputs")
|
||||
select el;
|
||||
|
||||
|
||||
|
||||
// WINDOWS EVENTS
|
||||
// WINDOWS EVENTSexc
|
||||
IEnumerable<XElement> xml_events =
|
||||
from el in inputs.Descendants("WindowsEvents").Descendants("Events")
|
||||
select el;
|
||||
@@ -420,8 +431,7 @@ namespace TimberWinR
|
||||
|
||||
Params_WindowsEvents args = parseParams_Events(e.Attributes());
|
||||
|
||||
|
||||
WindowsEvents evt = new WindowsEvents(source, fields, args);
|
||||
WindowsEvents evt = new WindowsEvents(source, fields, args);
|
||||
_events.Add(evt);
|
||||
}
|
||||
|
||||
@@ -526,7 +536,7 @@ namespace TimberWinR
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,14 +68,14 @@ namespace TimberWinR.Inputs
|
||||
{
|
||||
var record = rs.getRecord();
|
||||
var json = new JObject();
|
||||
foreach (var fieldName in _arguments.Fields)
|
||||
foreach (var field in _arguments.Fields)
|
||||
{
|
||||
object v = record.getValue(fieldName);
|
||||
object v = record.getValue(field.Name);
|
||||
|
||||
// if (field.FieldType == typeof(DateTime))
|
||||
// v = field.ToDateTime(v).ToUniversalTime();
|
||||
if (field.FieldType == typeof(DateTime))
|
||||
v = field.ToDateTime(v).ToUniversalTime();
|
||||
|
||||
json.Add(new JProperty(fieldName, v));
|
||||
json.Add(new JProperty(field.Name, v));
|
||||
}
|
||||
json.Add(new JProperty("type", "Win32-Eventlog"));
|
||||
ProcessJson(json.ToString());
|
||||
@@ -92,31 +92,5 @@ namespace TimberWinR.Inputs
|
||||
System.Threading.Thread.Sleep(_pollingIntervalInSeconds * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
public static FieldDefinitions FieldDefinitions
|
||||
{
|
||||
get
|
||||
{
|
||||
return new FieldDefinitions()
|
||||
{
|
||||
{"EventLog", typeof (string)},
|
||||
{"RecordNumber", typeof (string)},
|
||||
{"TimeGenerated", typeof (DateTime)},
|
||||
{"TimeWritten", typeof (DateTime)},
|
||||
{"EventID", typeof (int)},
|
||||
{"EventType", typeof (int)},
|
||||
{"EventTypeName", typeof (string)},
|
||||
{"EventCategory", typeof (int)},
|
||||
{"EventCategoryName", typeof (string)},
|
||||
{"SourceName", typeof (string)},
|
||||
{"Strings", typeof (string)},
|
||||
{"ComputerName", typeof (string)},
|
||||
{"SID", typeof (string)},
|
||||
{"Message", typeof (string)},
|
||||
{"Data", typeof (string)}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user