diff --git a/source/log4net-loggly/LogglyAppender.cs b/source/log4net-loggly/LogglyAppender.cs index c7fcd32..11f7993 100644 --- a/source/log4net-loggly/LogglyAppender.cs +++ b/source/log4net-loggly/LogglyAppender.cs @@ -12,9 +12,10 @@ public class LogglyAppender : AppenderSkeleton { List lstLogs = new List(); string[] arr = new string[100]; - public static readonly string InputKeyProperty = "LogglyInputKey"; - public static ILogglyFormatter Formatter = new LogglyFormatter(); - public static ILogglyClient Client = new LogglyClient(); + public readonly string InputKeyProperty = "LogglyInputKey"; + public ILogglyFormatter Formatter = new LogglyFormatter(); + public ILogglyClient Client = new LogglyClient(); + public LogglySendBufferedLogs _sendBufferedLogs = new LogglySendBufferedLogs(); private ILogglyAppenderConfig Config = new LogglyAppenderConfig(); public string RootUrl { set { Config.RootUrl = value; } } public string InputKey { set { Config.InputKey = value; } } @@ -42,8 +43,8 @@ void t_Elapsed(object sender, Timer.ElapsedEventArgs e) if (lstLogs.Count != 0) { SendAllEvents(lstLogs.ToArray()); - } - LogglySendBufferedLogs.sendBufferedLogsToLoggly(Config, Config.LogMode == "bulk/"); + } + _sendBufferedLogs.sendBufferedLogsToLoggly(Config, Config.LogMode == "bulk/"); } protected override void Append(LoggingEvent loggingEvent) diff --git a/source/log4net-loggly/LogglyAsyncHandler.cs b/source/log4net-loggly/LogglyAsyncHandler.cs index 4aa4ad4..d699015 100644 --- a/source/log4net-loggly/LogglyAsyncHandler.cs +++ b/source/log4net-loggly/LogglyAsyncHandler.cs @@ -11,9 +11,9 @@ class LogglyAsyncHandler protected bool IsRunning = false; //static list of all the queues the le appender might be managing. - private static ConcurrentBag> _allQueues = new ConcurrentBag>(); + private ConcurrentBag> _allQueues = new ConcurrentBag>(); - public static ILogglyClient Client = new LogglyClient(); + public ILogglyClient Client = new LogglyClient(); public LogglyAsyncHandler() { Queue = new BlockingCollection(); diff --git a/source/log4net-loggly/LogglyBufferringAppender.cs b/source/log4net-loggly/LogglyBufferringAppender.cs index 0edf281..b6f4825 100644 --- a/source/log4net-loggly/LogglyBufferringAppender.cs +++ b/source/log4net-loggly/LogglyBufferringAppender.cs @@ -5,10 +5,10 @@ namespace log4net.loggly { public class LogglyBufferringAppender : BufferingAppenderSkeleton { - public static readonly string InputKeyProperty = "LogglyInputKey"; + public readonly string InputKeyProperty = "LogglyInputKey"; - public static ILogglyFormatter Formatter = new LogglyFormatter(); - public static ILogglyClient Client = new LogglyClient(); + public ILogglyFormatter Formatter = new LogglyFormatter(); + public ILogglyClient Client = new LogglyClient(); private ILogglyAppenderConfig Config = new LogglyAppenderConfig(); diff --git a/source/log4net-loggly/LogglyClient.cs b/source/log4net-loggly/LogglyClient.cs index 052447d..e376c2e 100644 --- a/source/log4net-loggly/LogglyClient.cs +++ b/source/log4net-loggly/LogglyClient.cs @@ -8,24 +8,25 @@ namespace log4net.loggly { public class LogglyClient : ILogglyClient { - static bool isValidToken = true; + bool isValidToken = true; + public LogglyStoreLogsInBuffer _storeLogsInBuffer = new LogglyStoreLogsInBuffer(); - public static void setTokenValid(bool flag) + public void setTokenValid(bool flag) { isValidToken = flag; } - void storeLogs(string message, ILogglyAppenderConfig config, bool isBulk) + public void storeLogs(string message, ILogglyAppenderConfig config, bool isBulk) { List messageBulk = new List(); if (isBulk) { messageBulk = message.Split('\n').ToList(); - LogglyStoreLogsInBuffer.storeBulkLogs(config, messageBulk, isBulk); + _storeLogsInBuffer.storeBulkLogs(config, messageBulk, isBulk); } else { - LogglyStoreLogsInBuffer.storeInputLogs(config, message, isBulk); + _storeLogsInBuffer.storeInputLogs(config, message, isBulk); } } @@ -42,6 +43,9 @@ public virtual void Send(ILogglyAppenderConfig config, string message) string _tag = config.Tag; bool isBulk = config.LogMode.Contains("bulk"); + HttpWebResponse webResponse; + HttpWebRequest webRequest; + //keeping userAgent backward compatible if (!string.IsNullOrWhiteSpace(config.UserAgent)) { @@ -54,17 +58,17 @@ public virtual void Send(ILogglyAppenderConfig config, string message) try { var bytes = Encoding.UTF8.GetBytes(message); - var webRequest = CreateWebRequest(config, _tag); - - using (var dataStream = webRequest.GetRequestStream()) - { - dataStream.Write(bytes, 0, bytes.Length); - dataStream.Flush(); - dataStream.Close(); - } - var webResponse = webRequest.GetResponse(); - webResponse.Close(); - break; + webRequest = CreateWebRequest(config, _tag); + + using (var dataStream = webRequest.GetRequestStream()) + { + dataStream.Write(bytes, 0, bytes.Length); + dataStream.Flush(); + dataStream.Close(); + } + webResponse = (HttpWebResponse)webRequest.GetResponse(); + webResponse.Close(); + break; } catch (WebException e) @@ -94,6 +98,13 @@ public virtual void Send(ILogglyAppenderConfig config, string message) } } } + + finally + { + webRequest = null; + webResponse = null; + GC.Collect(); + } } } @@ -127,7 +138,8 @@ protected virtual HttpWebRequest CreateWebRequest(ILogglyAppenderConfig config, var url = String.Concat(config.RootUrl, config.LogMode, config.InputKey); //adding userAgent as tag in the log url = String.Concat(url, "/tag/" + tag); - var request = (HttpWebRequest)WebRequest.Create(url); + HttpWebRequest request = null; + request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ReadWriteTimeout = request.Timeout = config.TimeoutInSeconds * 1000; request.UserAgent = config.UserAgent; diff --git a/source/log4net-loggly/LogglyFormatter.cs b/source/log4net-loggly/LogglyFormatter.cs index a67b48b..2bbbe0a 100644 --- a/source/log4net-loggly/LogglyFormatter.cs +++ b/source/log4net-loggly/LogglyFormatter.cs @@ -221,7 +221,7 @@ private string GetMessageAndObjectInfo(LoggingEvent loggingEvent, out object obj { string message = string.Empty; objInfo = null; - int bytesLengthAllowdToLoggly = EVENT_SIZE; + int bytesLengthAllowedToLoggly = EVENT_SIZE; if (loggingEvent.MessageObject != null) { @@ -232,9 +232,9 @@ private string GetMessageAndObjectInfo(LoggingEvent loggingEvent, out object obj { message = loggingEvent.MessageObject.ToString(); int messageSizeInBytes = Encoding.Default.GetByteCount(message); - if (messageSizeInBytes > bytesLengthAllowdToLoggly) + if (messageSizeInBytes > bytesLengthAllowedToLoggly) { - message = message.Substring(0, bytesLengthAllowdToLoggly); + message = message.Substring(0, bytesLengthAllowedToLoggly); } } else diff --git a/source/log4net-loggly/LogglySendBufferedLogs.cs b/source/log4net-loggly/LogglySendBufferedLogs.cs index ff92266..19abec9 100644 --- a/source/log4net-loggly/LogglySendBufferedLogs.cs +++ b/source/log4net-loggly/LogglySendBufferedLogs.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -7,15 +7,15 @@ namespace log4net.loggly { public class LogglySendBufferedLogs { - public static string message; - public static List arrayMessage = new List(); - public static ILogglyClient Client = new LogglyClient(); + public string message = null; + public List arrayMessage = new List(); + public ILogglyClient Client = new LogglyClient(); + public LogglyClient _logClient = new LogglyClient(); - public static void sendBufferedLogsToLoggly(ILogglyAppenderConfig config, bool isBulk) + public void sendBufferedLogsToLoggly(ILogglyAppenderConfig config, bool isBulk) { if (LogglyStoreLogsInBuffer.arrBufferedMessage.Count > 0) { - int bulkModeBunch = 100; int inputModeBunch = 1; int logInBunch = isBulk ? bulkModeBunch : inputModeBunch; @@ -40,14 +40,18 @@ public static void sendBufferedLogsToLoggly(ILogglyAppenderConfig config, bool i var response = (HttpWebResponse)e.Response; if (response != null && response.StatusCode == HttpStatusCode.Forbidden) { - LogglyClient.setTokenValid(false); + _logClient.setTokenValid(false); Console.WriteLine("Loggly error: {0}", e.Message); return; } } - } + finally + { + arrayMessage.Clear(); + arrayMessage = null; + GC.Collect(); + } + } } - } } - diff --git a/source/log4net-loggly/LogglyStoreLogsInBuffer.cs b/source/log4net-loggly/LogglyStoreLogsInBuffer.cs index 8b63930..e4faa4a 100644 --- a/source/log4net-loggly/LogglyStoreLogsInBuffer.cs +++ b/source/log4net-loggly/LogglyStoreLogsInBuffer.cs @@ -1,15 +1,14 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; namespace log4net.loggly { - class LogglyStoreLogsInBuffer + public class LogglyStoreLogsInBuffer { public static List arrBufferedMessage = new List(); - public static List tempList = new List(); - public static void storeBulkLogs(ILogglyAppenderConfig config, List logs, bool isBulk) + public void storeBulkLogs(ILogglyAppenderConfig config, List logs, bool isBulk) { if (logs.Count == 0) return; int numberOfLogsToBeRemoved = (arrBufferedMessage.Count + logs.Count) - config.BufferSize; @@ -18,7 +17,7 @@ public static void storeBulkLogs(ILogglyAppenderConfig config, List logs arrBufferedMessage = logs.Concat(arrBufferedMessage).ToList(); } - public static void storeInputLogs(ILogglyAppenderConfig config, string message, bool isBulk) + public void storeInputLogs(ILogglyAppenderConfig config, string message, bool isBulk) { if (message == String.Empty) return; int numberOfLogsToBeRemoved = (arrBufferedMessage.Count + 1) - config.BufferSize; diff --git a/source/log4net-loggly/log4net-loggly.csproj b/source/log4net-loggly/log4net-loggly.csproj index 8a7dbcf..36b514e 100644 --- a/source/log4net-loggly/log4net-loggly.csproj +++ b/source/log4net-loggly/log4net-loggly.csproj @@ -31,13 +31,13 @@ 4 - - False - ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll + + ..\..\..\packages\log4net.2.0.7\lib\net40-full\log4net.dll + True - ..\packages\Newtonsoft.Json.8.0.1\lib\net40\Newtonsoft.Json.dll - True + ..\packages\Newtonsoft.Json.8.0.1\lib\net40\Newtonsoft.Json.dll + True @@ -67,10 +67,10 @@ - \ No newline at end of file + diff --git a/source/log4net-loggly/packages.config b/source/log4net-loggly/packages.config index cb0f1f4..a11abe5 100644 --- a/source/log4net-loggly/packages.config +++ b/source/log4net-loggly/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file