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

Always

Just for now

@@ -10,5 +10,6 @@ public interface ILogglyAppenderConfig
string Tag { get; set; }
string LogicalThreadContextKeys { get; set; }
string GlobalContextKeys { get; set; }
int BufferSize { get; set; }
}
}
@@ -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);
}
}
@@ -10,13 +10,13 @@ public interface ILogglyFormatter
string ToJson(LoggingEvent loggingEvent);
string ToJson(IEnumerable<LoggingEvent> loggingEvents);

/// <summary>
/// Merged Layout formatted log with the formatted timestamp
/// </summary>
/// <param name="renderedLog"></param>
/// <param name="timeStamp"></param>
/// <returns></returns>
string ToJson(string renderedLog, DateTime timeStamp);
/// <summary>
/// Merged Layout formatted log with the formatted timestamp
/// </summary>
/// <param name="renderedLog"></param>
/// <param name="timeStamp"></param>
/// <returns></returns>
string ToJson(string renderedLog, DateTime timeStamp);
}
}
@@ -24,6 +24,7 @@ public class LogglyAppender : AppenderSkeleton
public string Tag { set { Config.Tag = value; } }
public string LogicalThreadContextKeys { set { Config.LogicalThreadContextKeys = value; } }
public string GlobalContextKeys { set { Config.GlobalContextKeys = value; } }
public int BufferSize { set { Config.BufferSize = value; } }

private LogglyAsyncHandler LogglyAsync;

@@ -41,7 +42,8 @@ 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)
@@ -98,5 +100,5 @@ private void SendAllEvents(string[] events)
LogglyAsync.PostMessage(bulkLog, Config);
}

}
}
}
}
@@ -43,6 +43,7 @@ public string LogMode

public string GlobalContextKeys { get; set; }

public int BufferSize { get; set; }
public LogglyAppenderConfig()
{
UserAgent = "loggly-log4net-appender";
@@ -51,6 +52,7 @@ public LogglyAppenderConfig()
LogMode = "bulk";
LogicalThreadContextKeys = null;
GlobalContextKeys = null;
BufferSize = 500;
}
}
}
@@ -18,6 +18,7 @@ public class LogglyBufferringAppender : BufferingAppenderSkeleton
public string LogMode { set { Config.LogMode = value; } }
public int TimeoutInSeconds { set { Config.TimeoutInSeconds = value; } }
public string Tag { set { Config.Tag = value; } }
public int BufferSize { set { Config.BufferSize = value; } }

protected override void Append(LoggingEvent loggingEvent)
{
@@ -1,52 +1,132 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Linq;

namespace log4net.loggly
{
public class LogglyClient : ILogglyClient
{
static bool isValidToken = true;

public static void setTokenValid(bool flag)
{
isValidToken = flag;
}

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);
}
else
{
LogglyStoreLogsInBuffer.storeInputLogs(config, message, isBulk);
}
}

void printErrorMessage(string message)
{
Console.WriteLine("Loggly error: {0}", message);
}

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

string _tag = config.Tag;

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

while (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;
}
catch { }
}
int maxRetryAllowed = 5;
int totalRetries = 0;

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

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

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;
}

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);
}
}
}
}
}

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;
}
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)
{
var url = String.Concat(config.RootUrl, config.LogMode, config.InputKey);
//adding userAgent as tag in the log
url = String.Concat(url, "/tag/" + tag);
//adding userAgent as tag in the log
url = String.Concat(url, "/tag/" + tag);
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ReadWriteTimeout = request.Timeout = config.TimeoutInSeconds * 1000;
@@ -55,5 +135,5 @@ protected virtual HttpWebRequest CreateWebRequest(ILogglyAppenderConfig config,
request.ContentType = "application/json";
return request;
}
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.