From 7f6e56323830af4cae40769550742d66f79f59c8 Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Wed, 8 Apr 2015 10:58:27 -0400 Subject: [PATCH] Enhanced to log JSON errors to Redis/Elasticsearch --- TimberWinR.ServiceHost/Program.cs | 5 +++ TimberWinR.TestGenerator/Program.cs | 2 +- TimberWinR/Inputs/TcpInputListener.cs | 4 +++ TimberWinR/Inputs/UdpInputListener.cs | 7 +++- TimberWinR/LogErrors.cs | 48 +++++++++++++++++++++++++++ TimberWinR/TimberWinR.csproj | 1 + 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 TimberWinR/LogErrors.cs diff --git a/TimberWinR.ServiceHost/Program.cs b/TimberWinR.ServiceHost/Program.cs index 73d2b44..94ec34b 100644 --- a/TimberWinR.ServiceHost/Program.cs +++ b/TimberWinR.ServiceHost/Program.cs @@ -7,7 +7,9 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; +using System.Xml; using Microsoft.Win32; + using TimberWinR.Outputs; using TimberWinR.ServiceHost; using TimberWinR.Inputs; @@ -26,6 +28,9 @@ namespace TimberWinR.ServiceHost private static void Main(string[] args) { + + + Arguments arguments = new Arguments(); HostFactory.Run(hostConfigurator => diff --git a/TimberWinR.TestGenerator/Program.cs b/TimberWinR.TestGenerator/Program.cs index 18cc313..5711f74 100644 --- a/TimberWinR.TestGenerator/Program.cs +++ b/TimberWinR.TestGenerator/Program.cs @@ -15,6 +15,7 @@ using NLog; using NLog.Config; using NLog.Targets; using ServiceStack.Text.Jsv; +using TimberWinR.Parser; namespace TimberWinR.TestGenerator @@ -54,7 +55,6 @@ namespace TimberWinR.TestGenerator ramCounter.CategoryName = "Memory"; ramCounter.CounterName = "% Committed Bytes In Use"; - Options = new CommandLineOptions(); diff --git a/TimberWinR/Inputs/TcpInputListener.cs b/TimberWinR/Inputs/TcpInputListener.cs index 64db158..ae7a37e 100644 --- a/TimberWinR/Inputs/TcpInputListener.cs +++ b/TimberWinR/Inputs/TcpInputListener.cs @@ -113,6 +113,10 @@ namespace TimberWinR.Inputs } catch (Exception ex) { + var jex1 = LogErrors.LogException("Bad Json", ex); + if (jex1 != null) + ProcessJson(jex1); + LogManager.GetCurrentClassLogger().Warn(ex); } diff --git a/TimberWinR/Inputs/UdpInputListener.cs b/TimberWinR/Inputs/UdpInputListener.cs index 020cc23..d7f0cd7 100644 --- a/TimberWinR/Inputs/UdpInputListener.cs +++ b/TimberWinR/Inputs/UdpInputListener.cs @@ -96,9 +96,14 @@ namespace TimberWinR.Inputs break; } catch (Exception ex) - { + { + var jex1 = LogErrors.LogException(string.Format("Invalid JSON: {0}", lastMessage), ex); + if (jex1 != null) + ProcessJson(jex1); + LogManager.GetCurrentClassLogger().Warn("Bad JSON: {0}", lastMessage); LogManager.GetCurrentClassLogger().Warn(ex); + Interlocked.Increment(ref _parsedErrors); } } diff --git a/TimberWinR/LogErrors.cs b/TimberWinR/LogErrors.cs new file mode 100644 index 0000000..698f09a --- /dev/null +++ b/TimberWinR/LogErrors.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +using TimberWinR.Parser; + +namespace TimberWinR +{ + public class LogErrors + { + public static JObject LogException(Exception ex) + { + return LogException("Exception", ex); + } + + public static JObject LogException(string errorMessage, Exception ex) + { + JObject result = new JObject(); + result["type"] = "TimberWinR-Error"; + result["ErrorMessage"] = errorMessage; + + try + { + JsonConvert.DefaultSettings = () => new JsonSerializerSettings + { + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore + }; + + var exJson = JObject.Parse(JsonConvert.SerializeObject(ex)); + + result.Merge(exJson, new JsonMergeSettings + { + MergeArrayHandling = MergeArrayHandling.Replace + }); + return result; + } + catch (Exception ex1) + { + result["ErrorMessage"] = ex1.ToString(); + return result; + } + } + } +} diff --git a/TimberWinR/TimberWinR.csproj b/TimberWinR/TimberWinR.csproj index 4ad3c76..cd0fb73 100644 --- a/TimberWinR/TimberWinR.csproj +++ b/TimberWinR/TimberWinR.csproj @@ -111,6 +111,7 @@ +