From 7143ba7ab2b318ad778ea6ff5678509408cf67b6 Mon Sep 17 00:00:00 2001 From: Richard Nagle Date: Mon, 30 Oct 2017 15:26:24 +0000 Subject: [PATCH 1/4] add Flush() method to LogglyAsyncHandler to synchronously wait until all messages are sent --- source/log4net-loggly/LogglyAsyncHandler.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/log4net-loggly/LogglyAsyncHandler.cs b/source/log4net-loggly/LogglyAsyncHandler.cs index d699015..c9f361b 100644 --- a/source/log4net-loggly/LogglyAsyncHandler.cs +++ b/source/log4net-loggly/LogglyAsyncHandler.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Linq; using System.Threading; namespace log4net.loggly @@ -52,6 +53,14 @@ public void PostMessage(string msg, ILogglyAppenderConfig config) Queue.TryAdd(msg); } } + + public void Flush() + { + while (IsRunning && Queue.Any()) + { + Thread.SpinWait(100); + } + } } } \ No newline at end of file From 55ff69af33dc0f3e01aa05cc2e37d696f27de350 Mon Sep 17 00:00:00 2001 From: Richard Nagle Date: Mon, 30 Oct 2017 15:40:50 +0000 Subject: [PATCH 2/4] refactoring - extract methods IsBulkMode() and IsInputsMode() to reduce duplication --- source/log4net-loggly/LogglyAppender.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/source/log4net-loggly/LogglyAppender.cs b/source/log4net-loggly/LogglyAppender.cs index 11f7993..9075fba 100644 --- a/source/log4net-loggly/LogglyAppender.cs +++ b/source/log4net-loggly/LogglyAppender.cs @@ -44,7 +44,7 @@ void t_Elapsed(object sender, Timer.ElapsedEventArgs e) { SendAllEvents(lstLogs.ToArray()); } - _sendBufferedLogs.sendBufferedLogsToLoggly(Config, Config.LogMode == "bulk/"); + _sendBufferedLogs.sendBufferedLogsToLoggly(Config, IsBulkMode()); } protected override void Append(LoggingEvent loggingEvent) @@ -73,11 +73,11 @@ private void SendLogAction(LoggingEvent loggingEvent) } //check if logMode is bulk or inputs - if (Config.LogMode == "bulk/") + if (IsBulkMode()) { addToBulk(_formattedLog); } - else if (Config.LogMode == "inputs/") + else if (IsInputsMode()) { //sending _formattedLog to the async queue LogglyAsync.PostMessage(_formattedLog, Config); @@ -101,5 +101,14 @@ private void SendAllEvents(string[] events) LogglyAsync.PostMessage(bulkLog, Config); } - } - } \ No newline at end of file + private bool IsBulkMode() + { + return Config.LogMode == "bulk/"; + } + + private bool IsInputsMode() + { + return Config.LogMode == "inputs/"; + } + } +} \ No newline at end of file From 6820692417a3faf43c4e7b56d2f0b09781ccd211 Mon Sep 17 00:00:00 2001 From: Richard Nagle Date: Mon, 30 Oct 2017 15:43:31 +0000 Subject: [PATCH 3/4] override OnClose() in LogglyAppender so that it flushes all messages on shutdown --- source/log4net-loggly/LogglyAppender.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/source/log4net-loggly/LogglyAppender.cs b/source/log4net-loggly/LogglyAppender.cs index 9075fba..092092c 100644 --- a/source/log4net-loggly/LogglyAppender.cs +++ b/source/log4net-loggly/LogglyAppender.cs @@ -2,6 +2,7 @@ using log4net.Core; using System; using System.Collections.Generic; +using System.Linq; using Timer = System.Timers; @@ -110,5 +111,17 @@ private bool IsInputsMode() { return Config.LogMode == "inputs/"; } + + protected override void OnClose() + { + if (IsBulkMode() && lstLogs.Any()) + { + SendAllEvents(lstLogs.ToArray()); + } + + LogglyAsync.Flush(); + + base.OnClose(); + } } } \ No newline at end of file From 2325093bec68ee6458db8177af9368fd1440f158 Mon Sep 17 00:00:00 2001 From: Richard Nagle Date: Mon, 30 Oct 2017 16:13:21 +0000 Subject: [PATCH 4/4] update readme to remove comment about waiting for threads to finish --- README.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/README.md b/README.md index 01f6a7a..eeac9f9 100644 --- a/README.md +++ b/README.md @@ -69,14 +69,6 @@ Send logs to Loggly using the following code logger.Info("log message"); ``` -For Console Application - -You should add the following statement at the end of your Main method as the log4net-loggly library is asynchronous so there needs to be time for the threads the complete logging before the application exits. - -``` -Console.ReadKey(); -``` - Added handling for LoggingEvent properties Support for properties tied to a specific event and not a ThreadContext which is shared across the entire thread.