Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify static fields/methods to non-static for less memory consumption #4

Merged
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -12,9 +12,10 @@ public class LogglyAppender : AppenderSkeleton
{
List<string> lstLogs = new List<string>();
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)
@@ -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<BlockingCollection<string>> _allQueues = new ConcurrentBag<BlockingCollection<string>>();
private ConcurrentBag<BlockingCollection<string>> _allQueues = new ConcurrentBag<BlockingCollection<string>>();

public static ILogglyClient Client = new LogglyClient();
public ILogglyClient Client = new LogglyClient();
public LogglyAsyncHandler()
{
Queue = new BlockingCollection<string>();
@@ -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();

@@ -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<string> messageBulk = new List<string>();
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;
@@ -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
@@ -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<string> arrayMessage = new List<string>();
public static ILogglyClient Client = new LogglyClient();
public string message = null;
public List<string> arrayMessage = new List<string>();
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();
}
}
}

}
}

@@ -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<string> arrBufferedMessage = new List<string>();
public static List<string> tempList = new List<string>();

public static void storeBulkLogs(ILogglyAppenderConfig config, List<string> logs, bool isBulk)
public void storeBulkLogs(ILogglyAppenderConfig config, List<string> 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<string> 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;
@@ -31,13 +31,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
<Reference Include="log4net, Version=2.0.7.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\log4net.2.0.7\lib\net40-full\log4net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\Newtonsoft.Json.8.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
@@ -67,10 +67,10 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net4" />
<package id="log4net" version="2.0.7" targetFramework="net40" />
<package id="Newtonsoft.Json" version="8.0.1" targetFramework="net4" />
</packages>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.