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. diff --git a/source/log4net-loggly/LogglyAppender.cs b/source/log4net-loggly/LogglyAppender.cs index 11f7993..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; @@ -44,7 +45,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 +74,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 +102,26 @@ 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/"; + } + + protected override void OnClose() + { + if (IsBulkMode() && lstLogs.Any()) + { + SendAllEvents(lstLogs.ToArray()); + } + + LogglyAsync.Flush(); + + base.OnClose(); + } + } +} \ No newline at end of file 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