diff --git a/source/log4net-loggly-console/App.config b/source/log4net-loggly-console/App.config index 9a0b987..c93dc2f 100644 --- a/source/log4net-loggly-console/App.config +++ b/source/log4net-loggly-console/App.config @@ -20,12 +20,12 @@ - - + + - - + + \ No newline at end of file diff --git a/source/log4net-loggly/LogglyAppender.cs b/source/log4net-loggly/LogglyAppender.cs index 11f0dd2..7abb4cd 100644 --- a/source/log4net-loggly/LogglyAppender.cs +++ b/source/log4net-loggly/LogglyAppender.cs @@ -1,4 +1,7 @@ -using log4net.Appender; +using System; +using System.Diagnostics; +using System.Threading; +using log4net.Appender; using log4net.Core; namespace log4net.loggly @@ -19,8 +22,24 @@ public class LogglyAppender : AppenderSkeleton protected override void Append(LoggingEvent loggingEvent) { - Formatter.AppendAdditionalLoggingInformation(Config, loggingEvent); - Client.Send(Config, Config.InputKey, Formatter.ToJson(loggingEvent)); + ThreadPool.QueueUserWorkItem(x => + { + try + { + Formatter.AppendAdditionalLoggingInformation(Config, loggingEvent); + Client.Send(Config, Config.InputKey, Formatter.ToJson(loggingEvent)); + } + catch (Exception ex) + { + Console.Out.WriteLine(ex.ToString()); +#if DEBUG + Debug.WriteLine("loggly-log4net error \r\n:" + ex.ToString()); +#endif + Trace.WriteLine("loggly-log4net error \r\n:" + ex.ToString()); + } + }); } + + } } diff --git a/source/log4net-loggly/LogglyClient.cs b/source/log4net-loggly/LogglyClient.cs index db6b8cb..0b8aa94 100644 --- a/source/log4net-loggly/LogglyClient.cs +++ b/source/log4net-loggly/LogglyClient.cs @@ -6,10 +6,12 @@ namespace log4net.loggly { public class LogglyClient : ILogglyClient { + public virtual void Send(ILogglyAppenderConfig config, string inputKey, string message) { var bytes = Encoding.UTF8.GetBytes(message); var request = CreateWebRequest(config, string.IsNullOrWhiteSpace(inputKey) ? config.InputKey : inputKey); + using (var dataStream = request.GetRequestStream()) { dataStream.Write(bytes, 0, bytes.Length); diff --git a/source/log4net-loggly/LogglyFormatter.cs b/source/log4net-loggly/LogglyFormatter.cs index b4d1e8e..dc978b4 100644 --- a/source/log4net-loggly/LogglyFormatter.cs +++ b/source/log4net-loggly/LogglyFormatter.cs @@ -7,46 +7,51 @@ namespace log4net.loggly { - public class LogglyFormatter : ILogglyFormatter - { - private Process _currentProcess; + public class LogglyFormatter : ILogglyFormatter + { + private Process _currentProcess; - public LogglyFormatter() - { - _currentProcess = Process.GetCurrentProcess(); - } + public LogglyFormatter() + { + _currentProcess = Process.GetCurrentProcess(); + } - public virtual void AppendAdditionalLoggingInformation(ILogglyAppenderConfig config, LoggingEvent loggingEvent) - { - } + public virtual void AppendAdditionalLoggingInformation(ILogglyAppenderConfig config, LoggingEvent loggingEvent) + { + } - public virtual string ToJson(LoggingEvent loggingEvent) - { - return PreParse(loggingEvent).ToJson(); - } + public virtual string ToJson(LoggingEvent loggingEvent) + { + return PreParse(loggingEvent).ToJson(); + } - public virtual string ToJson(IEnumerable loggingEvents) - { - return loggingEvents.Select(PreParse).ToJson(); - } + public virtual string ToJson(IEnumerable loggingEvents) + { + return loggingEvents.Select(PreParse).ToJson(); + } - private object PreParse(LoggingEvent loggingEvent) - { - var exceptionString = loggingEvent.GetExceptionString(); - if (string.IsNullOrWhiteSpace(exceptionString)) - { - exceptionString = null; //ensure empty strings aren't included in the json output. - } - return new - { - level = loggingEvent.Level.DisplayName, - time = loggingEvent.TimeStamp.ToString("yyyyMMdd HHmmss.fff zzz"), - machine = Environment.MachineName, - process = _currentProcess.ProcessName, - thread = loggingEvent.ThreadName, - message = loggingEvent.MessageObject, - ex = exceptionString, - }; - } - } + private object PreParse(LoggingEvent loggingEvent) + { + var exceptionString = loggingEvent.GetExceptionString(); + if (string.IsNullOrWhiteSpace(exceptionString)) + { + exceptionString = null; //ensure empty strings aren't included in the json output. + } + + var result = new + { + level = loggingEvent.Level.DisplayName, + time = loggingEvent.TimeStamp.ToString("yyyyMMdd HHmmss.fff zzz"), + machine = Environment.MachineName, + process = _currentProcess.ProcessName, + thread = loggingEvent.ThreadName, + message = loggingEvent.MessageObject.ToString(), + loggerName = loggingEvent.LoggerName, + pid = GlobalContext.Properties["pid"], + ex = exceptionString, + }; + + return result; + } + } } \ No newline at end of file diff --git a/source/log4net-loggly/log4net-loggly.csproj b/source/log4net-loggly/log4net-loggly.csproj index a49567a..4392ed9 100644 --- a/source/log4net-loggly/log4net-loggly.csproj +++ b/source/log4net-loggly/log4net-loggly.csproj @@ -31,8 +31,9 @@ 4 - - ..\packages\log4net.2.0.0\lib\net40-full\log4net.dll + + False + ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll ..\packages\ServiceStack.Text.3.8.5\lib\net35\ServiceStack.Text.dll diff --git a/source/log4net-loggly/packages.config b/source/log4net-loggly/packages.config index 55d0a20..a1b2257 100644 --- a/source/log4net-loggly/packages.config +++ b/source/log4net-loggly/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/source/packages/repositories.config b/source/packages/repositories.config index e52a69d..6f8f19f 100644 --- a/source/packages/repositories.config +++ b/source/packages/repositories.config @@ -2,5 +2,4 @@ - \ No newline at end of file