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

Buffer support in log4net-loggly library during network outage #3

Merged
merged 4 commits into from Jan 28, 2017
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

initial commit for buffer support in log4net-loggly library

  • Loading branch information
Shwetajain148 committed Jan 20, 2017
commit 4563a11bb20a6ed2d19f8a97c2091655b2b44f21
@@ -2,6 +2,7 @@ namespace log4net.loggly
{
public interface ILogglyClient
{
void Send(ILogglyAppenderConfig config, string message);
void Send(ILogglyAppenderConfig config, string message);
void Send(ILogglyAppenderConfig config, string message, bool isBulk);
}
}
@@ -8,95 +8,96 @@

namespace log4net.loggly
{
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();
private ILogglyAppenderConfig Config = new LogglyAppenderConfig();
public string RootUrl { set { Config.RootUrl = value; } }
public string InputKey { set { Config.InputKey = value; } }
public string UserAgent { set { Config.UserAgent = value; } }
public string LogMode { set { Config.LogMode = value; } }
public int TimeoutInSeconds { set { Config.TimeoutInSeconds = value; } }
public string Tag { set { Config.Tag = value; } }
public string LogicalThreadContextKeys { set { Config.LogicalThreadContextKeys = value; } }
public string GlobalContextKeys { set { Config.GlobalContextKeys = value; } }
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();
private ILogglyAppenderConfig Config = new LogglyAppenderConfig();
public string RootUrl { set { Config.RootUrl = value; } }
public string InputKey { set { Config.InputKey = value; } }
public string UserAgent { set { Config.UserAgent = value; } }
public string LogMode { set { Config.LogMode = value; } }
public int TimeoutInSeconds { set { Config.TimeoutInSeconds = value; } }
public string Tag { set { Config.Tag = value; } }
public string LogicalThreadContextKeys { set { Config.LogicalThreadContextKeys = value; } }
public string GlobalContextKeys { set { Config.GlobalContextKeys = value; } }

private LogglyAsyncHandler LogglyAsync;
private LogglyAsyncHandler LogglyAsync;

public LogglyAppender()
{
LogglyAsync = new LogglyAsyncHandler();
Timer.Timer t = new Timer.Timer();
t.Interval = 5000;
t.Enabled = true;
t.Elapsed += t_Elapsed;
}
public LogglyAppender()
{
LogglyAsync = new LogglyAsyncHandler();
Timer.Timer t = new Timer.Timer();
t.Interval = 5000;
t.Enabled = true;
t.Elapsed += t_Elapsed;
}

void t_Elapsed(object sender, Timer.ElapsedEventArgs e)
{
if (lstLogs.Count != 0)
{
SendAllEvents(lstLogs.ToArray());
}
}
void t_Elapsed(object sender, Timer.ElapsedEventArgs e)
{
if (lstLogs.Count != 0)
{
SendAllEvents(lstLogs.ToArray());
}
LogglySendBufferedLogs.sendBufferedLogsToLoggly(Config, Config.LogMode == "bulk/");
}

protected override void Append(LoggingEvent loggingEvent)
{
SendLogAction(loggingEvent);
}
protected override void Append(LoggingEvent loggingEvent)
{
SendLogAction(loggingEvent);
}

private void SendLogAction(LoggingEvent loggingEvent)
{
//we should always format event in the same thread as
//many properties used in the event are associated with the current thread
//like threadname, ndc stacks, threadcontent properties etc.
private void SendLogAction(LoggingEvent loggingEvent)
{
//we should always format event in the same thread as
//many properties used in the event are associated with the current thread
//like threadname, ndc stacks, threadcontent properties etc.

//initializing a string for the formatted log
string _formattedLog = string.Empty;
//initializing a string for the formatted log
string _formattedLog = string.Empty;

//if Layout is null then format the log from the Loggly Client
if (this.Layout == null)
{
Formatter.AppendAdditionalLoggingInformation(Config, loggingEvent);
_formattedLog = Formatter.ToJson(loggingEvent);
}
else
{
_formattedLog = Formatter.ToJson(RenderLoggingEvent(loggingEvent), loggingEvent.TimeStamp);
}
//if Layout is null then format the log from the Loggly Client
if (this.Layout == null)
{
Formatter.AppendAdditionalLoggingInformation(Config, loggingEvent);
_formattedLog = Formatter.ToJson(loggingEvent);
}
else
{
_formattedLog = Formatter.ToJson(RenderLoggingEvent(loggingEvent), loggingEvent.TimeStamp);
}

//check if logMode is bulk or inputs
if (Config.LogMode == "bulk/")
{
addToBulk(_formattedLog);
}
else if (Config.LogMode == "inputs/")
{
//sending _formattedLog to the async queue
LogglyAsync.PostMessage(_formattedLog, Config);
}
}
//check if logMode is bulk or inputs
if (Config.LogMode == "bulk/")
{
addToBulk(_formattedLog);
}
else if (Config.LogMode == "inputs/")
{
//sending _formattedLog to the async queue
LogglyAsync.PostMessage(_formattedLog, Config);
}
}

public void addToBulk(string log)
{
// store all events into a array max lenght is 100
lstLogs.Add(log.Replace("\n", ""));
if (lstLogs.Count == 100)
{
SendAllEvents(lstLogs.ToArray());
}
}
public void addToBulk(string log)
{
// store all events into a array max lenght is 100
lstLogs.Add(log.Replace("\n", ""));
if (lstLogs.Count == 100)
{
SendAllEvents(lstLogs.ToArray());
}
}

private void SendAllEvents(string[] events)
{
lstLogs.Clear();
String bulkLog = String.Join(System.Environment.NewLine, events);
LogglyAsync.PostMessage(bulkLog, Config);
}
private void SendAllEvents(string[] events)
{
lstLogs.Clear();
String bulkLog = String.Join(System.Environment.NewLine, events);
LogglyAsync.PostMessage(bulkLog, Config);
}

}
}
}
}
@@ -1,46 +1,105 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Linq;

namespace log4net.loggly
{
public class LogglyClient : ILogglyClient
{
public virtual void Send(ILogglyAppenderConfig config, string message)
{
static bool isValidToken = true;
public static void setValidInvalidFlag(bool flag)
{
isValidToken = flag;
}

public virtual void Send(ILogglyAppenderConfig config, string message)
{
int maxRetryAllowed = 5;
int totalRetries = 0;

string _tag = config.Tag;

bool isBulk = config.LogMode.Contains("bulk");

List<string> messageBulk = new List<string>();
//keeping userAgent backward compatible
if (!string.IsNullOrWhiteSpace(config.UserAgent))
{
_tag = _tag + "," + config.UserAgent;
}

while (totalRetries < maxRetryAllowed)
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;
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;
}

catch (WebException e) {
var response = (HttpWebResponse)e.Response;
if (response != null)
{
if (response.StatusCode == HttpStatusCode.Forbidden) //Check for bad token
{
setValidInvalidFlag(false);
}
if (totalRetries == 1) Console.WriteLine("Loggly error: {0}", e.Message);
}

else if (totalRetries == 1)
{
if (isBulk)
{
messageBulk = message.Split('\n').ToList();
LogglyStoreLogsInBuffer.storeBulkLogs(config, messageBulk, isBulk);
}
else
{
LogglyStoreLogsInBuffer.storeInputLogs(config, message, isBulk);
}
}
}
}
}

public void Send(ILogglyAppenderConfig config, string message, bool isbulk)
{
if (isValidToken)
{
string _tag = config.Tag;

//keeping userAgent backward compatible
if (!string.IsNullOrWhiteSpace(config.UserAgent))
{
_tag = _tag + "," + config.UserAgent;
}
catch { }
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 = (HttpWebResponse)webRequest.GetResponse();
webResponse.Close();
}
}
}

protected virtual HttpWebRequest CreateWebRequest(ILogglyAppenderConfig config, string tag)
{
@@ -55,5 +114,5 @@ protected virtual HttpWebRequest CreateWebRequest(ILogglyAppenderConfig config,
request.ContentType = "application/json";
return request;
}
}
}
}
@@ -6,13 +6,15 @@
using Newtonsoft.Json;
using System.Dynamic;
using Newtonsoft.Json.Linq;
using System.Text;

namespace log4net.loggly
{
public class LogglyFormatter : ILogglyFormatter
{
private Process _currentProcess;
private ILogglyAppenderConfig _config;
public int EVENT_SIZE = 1000 * 1000;

public LogglyFormatter()
{
@@ -219,6 +221,7 @@ private string GetMessageAndObjectInfo(LoggingEvent loggingEvent, out object obj
{
string message = string.Empty;
objInfo = null;
int bytesLengthAllowdToLoggly = EVENT_SIZE;

if (loggingEvent.MessageObject != null)
{
@@ -228,6 +231,11 @@ private string GetMessageAndObjectInfo(LoggingEvent loggingEvent, out object obj
|| loggingEvent.MessageObject.GetType().FullName.Contains("StringFormatFormattedMessage"))
{
message = loggingEvent.MessageObject.ToString();
int messageSizeInBytes = Encoding.Default.GetByteCount(message);
if (messageSizeInBytes > bytesLengthAllowdToLoggly)
{
message = message.Substring(0, bytesLengthAllowdToLoggly);
}
}
else
{
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.