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

refactor: convert spaces to tab space

  • Loading branch information
Shwetajain148 committed Jan 24, 2017
commit ec9c3517e5a06af65c59944e38e2d911b788268c
@@ -10,6 +10,6 @@ public interface ILogglyAppenderConfig
string Tag { get; set; }
string LogicalThreadContextKeys { get; set; }
string GlobalContextKeys { get; set; }
int BufferSize { get; set; }
int BufferSize { get; set; }
}
}
@@ -2,7 +2,7 @@ namespace log4net.loggly
{
public interface ILogglyClient
{
void Send(ILogglyAppenderConfig config, string message);
void Send(ILogglyAppenderConfig config, string message, bool isBulk);
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);
}
}
@@ -8,97 +8,97 @@

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 int BufferSize { set { Config.BufferSize = 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; } }
public int BufferSize { set { Config.BufferSize = 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());
}
LogglySendBufferedLogs.sendBufferedLogsToLoggly(Config, Config.LogMode == "bulk/");
}
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);
}

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

public string GlobalContextKeys { get; set; }

public int BufferSize { get; set; }
public int BufferSize { get; set; }
public LogglyAppenderConfig()
{
UserAgent = "loggly-log4net-appender";
@@ -52,7 +52,7 @@ public LogglyAppenderConfig()
LogMode = "bulk";
LogicalThreadContextKeys = null;
GlobalContextKeys = null;
BufferSize = 500;
BufferSize = 500;
}
}
}
@@ -18,7 +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; } }
public int BufferSize { set { Config.BufferSize = value; } }

protected override void Append(LoggingEvent loggingEvent)
{
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.