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 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

modify static fields/methods to non-static and enhanced memory consum…

…ption
  • Loading branch information
shweta
shweta committed Feb 28, 2017
commit 3ca3456f607d8acce67e0219691a4704a350af03
@@ -8,13 +8,14 @@

namespace log4net.loggly
{
public class LogglyAppender : AppenderSkeleton
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; } }
@@ -43,7 +44,7 @@ void t_Elapsed(object sender, Timer.ElapsedEventArgs e)
{
SendAllEvents(lstLogs.ToArray());
}
LogglySendBufferedLogs.sendBufferedLogsToLoggly(Config, Config.LogMode == "bulk/");
_sendBufferedLogs.sendBufferedLogsToLoggly(Config, Config.LogMode == "bulk/");
}

protected override void Append(LoggingEvent loggingEvent)
@@ -97,7 +98,7 @@ private void SendAllEvents(string[] events)
{
lstLogs.Clear();
String bulkLog = String.Join(System.Environment.NewLine, events);
LogglyAsync.PostMessage(bulkLog, Config);
LogglyAsync.PostMessage(bulkLog, Config);
}

}
@@ -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))
{
@@ -51,49 +55,56 @@ public virtual void Send(ILogglyAppenderConfig config, string message)
while (isValidToken && totalRetries < maxRetryAllowed)
{
totalRetries++;
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;
}
try
{
var bytes = Encoding.UTF8.GetBytes(message);
webRequest = CreateWebRequest(config, _tag);

catch (WebException e)
{
if (totalRetries == 1)
{
var response = (HttpWebResponse)e.Response;
if (response != null)
{
// Check for bad token
if (response.StatusCode == HttpStatusCode.Forbidden)
{
// set valid token flag to false
setTokenValid(false);
}
else
{
// store logs to buffer
storeLogs(message, config, isBulk);
}
printErrorMessage(e.Message);
}
else
{
// store logs to buffer
storeLogs(message, config, isBulk);
}
}
}
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)
{
if (totalRetries == 1)
{
var response = (HttpWebResponse)e.Response;
if (response != null)
{
// Check for bad token
if (response.StatusCode == HttpStatusCode.Forbidden)
{
// set valid token flag to false
setTokenValid(false);
}
else
{
// store logs to buffer
storeLogs(message, config, isBulk);
}
printErrorMessage(e.Message);
}
else
{
// store logs to buffer
storeLogs(message, config, isBulk);
}
}
}

finally
{
webRequest = null;
webResponse = null;
GC.Collect();
}
}
}

@@ -109,15 +120,15 @@ public void Send(ILogglyAppenderConfig config, string message, bool isbulk)
_tag = _tag + "," + config.UserAgent;
}
var bytes = Encoding.UTF8.GetBytes(message);
var webRequest = CreateWebRequest(config, _tag);
var webRequest = CreateWebRequest(config, _tag);

using (var dataStream = webRequest.GetRequestStream())
{
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Flush();
dataStream.Close();
}
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var webResponse = (HttpWebResponse)webRequest.GetResponse();
webResponse.Close();
}
}
@@ -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
@@ -7,47 +7,53 @@ 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 LogglyStoreLogsInBuffer _storeEventsInBuffer = new LogglyStoreLogsInBuffer();

public static void sendBufferedLogsToLoggly(ILogglyAppenderConfig config, bool isBulk)
public void sendBufferedLogsToLoggly(ILogglyAppenderConfig config, bool isBulk)
{
if (LogglyStoreLogsInBuffer.arrBufferedMessage.Count > 0)
if (_storeEventsInBuffer.arrBufferedMessage.Count > 0)
{

int bulkModeBunch = 100;
int inputModeBunch = 1;
int logInBunch = isBulk ? bulkModeBunch : inputModeBunch;
arrayMessage = LogglyStoreLogsInBuffer.arrBufferedMessage.Take(logInBunch).ToList();
arrayMessage = _storeEventsInBuffer.arrBufferedMessage.Take(logInBunch).ToList();
message = isBulk ? String.Join(System.Environment.NewLine, arrayMessage) : arrayMessage[0];
try
{
Client.Send(config, message, isBulk);
var tempList = LogglyStoreLogsInBuffer.arrBufferedMessage;
if (LogglyStoreLogsInBuffer.arrBufferedMessage.Count < arrayMessage.Count)
var tempList = _storeEventsInBuffer.arrBufferedMessage;
if (_storeEventsInBuffer.arrBufferedMessage.Count < arrayMessage.Count)
{
LogglyStoreLogsInBuffer.arrBufferedMessage.Clear();
_storeEventsInBuffer.arrBufferedMessage.Clear();
}
else
{
tempList.RemoveRange(0, arrayMessage.Count);
}
LogglyStoreLogsInBuffer.arrBufferedMessage = tempList;
_storeEventsInBuffer.arrBufferedMessage = tempList;
}
catch (WebException e)
{
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();
}
}
}

}
}

@@ -4,12 +4,11 @@

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 List<string> arrBufferedMessage = 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;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.