forked from daptiv/log4net-loggly
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from psquickitjayant/develop
Added support for bulk mode
- Loading branch information
Showing
8 changed files
with
182 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,102 @@ | ||
using log4net.Appender; | ||
using log4net.Core; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System; | ||
using System.Collections.Generic; | ||
using Timer = System.Timers; | ||
|
||
|
||
|
||
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; } } | ||
|
||
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. | ||
|
||
//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); | ||
} | ||
|
||
//sending _formattedLog to the async queue | ||
ThreadPool.QueueUserWorkItem(x => Client.Send(Config, _formattedLog)); | ||
} | ||
public string Tag { set { Config.Tag = value; } } | ||
public string LogicalThreadContextKeys { set { Config.LogicalThreadContextKeys = value; } } | ||
public string GlobalContextKeys { set { Config.GlobalContextKeys = value; } } | ||
|
||
private LogglyAsyncHandler LogglyAsync; | ||
|
||
public LogglyAppender() | ||
{ | ||
LogglyAsync = new LogglyAsyncHandler(); | ||
Timer.Timer t = new Timer.Timer(); | ||
t.Interval = 20000; | ||
t.Enabled = true; | ||
t.Elapsed += t_Elapsed; | ||
} | ||
|
||
void t_Elapsed(object sender, Timer.ElapsedEventArgs e) | ||
{ | ||
if (lstLogs.Count != 0) | ||
{ | ||
SendAllEvents(lstLogs.ToArray()); | ||
} | ||
} | ||
|
||
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. | ||
|
||
//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); | ||
} | ||
|
||
//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()); | ||
} | ||
} | ||
|
||
private void SendAllEvents(string[] events) | ||
{ | ||
lstLogs.Clear(); | ||
String bulkLog = String.Join(System.Environment.NewLine, events); | ||
LogglyAsync.PostMessage(bulkLog, Config); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Collections.Concurrent; | ||
using System.Threading; | ||
|
||
namespace log4net.loggly | ||
{ | ||
class LogglyAsyncHandler | ||
{ | ||
// Size of the internal event queue. | ||
//protected const int QueueSize = 32768; | ||
protected ILogglyAppenderConfig _config; | ||
|
||
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>>(); | ||
|
||
public static ILogglyClient Client = new LogglyClient(); | ||
public LogglyAsyncHandler() | ||
{ | ||
Queue = new BlockingCollection<string>(); | ||
_allQueues.Add(Queue); | ||
WorkerThread = new Thread(new ThreadStart(SendLogs)); | ||
WorkerThread.Name = "Loggly Log Appender"; | ||
WorkerThread.IsBackground = true; | ||
} | ||
|
||
protected readonly BlockingCollection<string> Queue; | ||
protected Thread WorkerThread; | ||
|
||
protected virtual void SendLogs() | ||
{ | ||
if (this._config != null) | ||
{ | ||
while (true) | ||
{ | ||
var msg = Queue.Take(); | ||
Client.Send(this._config, msg); | ||
} | ||
} | ||
} | ||
|
||
public void PostMessage(string msg, ILogglyAppenderConfig config) | ||
{ | ||
this._config = config; | ||
if (!IsRunning) | ||
{ | ||
WorkerThread.Start(); | ||
IsRunning = true; | ||
} | ||
if (!Queue.TryAdd(msg)) | ||
{ | ||
Queue.Take(); | ||
Queue.TryAdd(msg); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters