SumoLogic / sumologic-net-appenders

Several appenders for .NET developers to use that send logs straight to SumoLogic.
Apache License 2.0
23 stars 39 forks source link

Allow the ability to flush any remaining logs that are buffered and haven't been sent #68

Closed jakejscott closed 5 years ago

jakejscott commented 5 years ago

If using the BufferedSumoLogicAppender when it shuts down logs are not being flushed at all. With this fix you can get all the IFlushable objects and call flush on them.

public static void Main()
        {
            try
            {
                int numMessages = 3;
                for (int i = 0; i < numMessages; i++)
                {
                    log4netLog.Info("info " + i);
                }
            }
            finally
            {
                FlushLogs();
            }

            Console.ReadKey();
        }

        private static void FlushLogs()
        {
            var appenderSkeletons = LogManager
                .GetAllRepositories()
                .SelectMany(r => r.GetAppenders())
                .OfType<IFlushable>()
                .ToArray();

            foreach (var appender in appenderSkeletons)
            {
                try
                {
                    appender.Flush(10000);
                }
                catch
                {
                    // Cannot do anything here
                }
            }
        }
bin3377 commented 5 years ago

Thanks for the contribution! The change is make sense for me. I will do some test and merge it

jakejscott commented 5 years ago

Are you going to publish a new nuget package?

bin3377 commented 5 years ago

yes. it's on https://www.nuget.org/packages/SumoLogic.Logging.Serilog/ now but I think I may push another version to do some adjustment like using consistent version number format...

jakejscott commented 5 years ago

This commit was a change for log4net :) Which hasn't been updated yet in Nuget https://www.nuget.org/packages/SumoLogic.Logging.Log4Net/

bin3377 commented 5 years ago

ahh... sorry I just confused myself. Yes, will push a new version for all packages after I fix the appveyor building and couple of doc nits

bin3377 commented 5 years ago

FYI - 1.0.0.8 nuget released with this change