11using log4net . Appender ;
22using log4net . Core ;
3- using System . Threading ;
4- using System . Threading . Tasks ;
3+ using System ;
54using System . Collections . Generic ;
5+ using Timer = System . Timers ;
6+
67
78
89namespace log4net . loggly
910{
1011 public class LogglyAppender : AppenderSkeleton
1112 {
13+ List < string > lstLogs = new List < string > ( ) ;
14+ string [ ] arr = new string [ 100 ] ;
1215 public static readonly string InputKeyProperty = "LogglyInputKey" ;
13-
1416 public static ILogglyFormatter Formatter = new LogglyFormatter ( ) ;
1517 public static ILogglyClient Client = new LogglyClient ( ) ;
16-
1718 private ILogglyAppenderConfig Config = new LogglyAppenderConfig ( ) ;
18-
1919 public string RootUrl { set { Config . RootUrl = value ; } }
2020 public string InputKey { set { Config . InputKey = value ; } }
2121 public string UserAgent { set { Config . UserAgent = value ; } }
22+ public string LogMode { set { Config . LogMode = value ; } }
2223 public int TimeoutInSeconds { set { Config . TimeoutInSeconds = value ; } }
23- public string Tag { set { Config . Tag = value ; } }
24- public string LogicalThreadContextKeys { set { Config . LogicalThreadContextKeys = value ; } }
25- public string GlobalContextKeys { set { Config . GlobalContextKeys = value ; } }
26-
27- protected override void Append ( LoggingEvent loggingEvent )
28- {
29- SendLogAction ( loggingEvent ) ;
30- }
31-
32- private void SendLogAction ( LoggingEvent loggingEvent )
33- {
34- //we should always format event in the same thread as
35- //many properties used in the event are associated with the current thread
36- //like threadname, ndc stacks, threadcontent properties etc.
37-
38- //initializing a string for the formatted log
39- string _formattedLog = string . Empty ;
40-
41- //if Layout is null then format the log from the Loggly Client
42- if ( this . Layout == null )
43- {
44- Formatter . AppendAdditionalLoggingInformation ( Config , loggingEvent ) ;
45- _formattedLog = Formatter . ToJson ( loggingEvent ) ;
46- }
47- else
48- {
49- _formattedLog = Formatter . ToJson ( RenderLoggingEvent ( loggingEvent ) , loggingEvent . TimeStamp ) ;
50- }
51-
52- //sending _formattedLog to the async queue
53- ThreadPool . QueueUserWorkItem ( x => Client . Send ( Config , _formattedLog ) ) ;
54- }
24+ public string Tag { set { Config . Tag = value ; } }
25+ public string LogicalThreadContextKeys { set { Config . LogicalThreadContextKeys = value ; } }
26+ public string GlobalContextKeys { set { Config . GlobalContextKeys = value ; } }
27+
28+ private LogglyAsyncHandler LogglyAsync ;
29+
30+ public LogglyAppender ( )
31+ {
32+ LogglyAsync = new LogglyAsyncHandler ( ) ;
33+ Timer . Timer t = new Timer . Timer ( ) ;
34+ t . Interval = 20000 ;
35+ t . Enabled = true ;
36+ t . Elapsed += t_Elapsed ;
37+ }
38+
39+ void t_Elapsed ( object sender , Timer . ElapsedEventArgs e )
40+ {
41+ if ( lstLogs . Count != 0 )
42+ {
43+ SendAllEvents ( lstLogs . ToArray ( ) ) ;
44+ }
45+ }
46+
47+ protected override void Append ( LoggingEvent loggingEvent )
48+ {
49+ SendLogAction ( loggingEvent ) ;
50+ }
51+
52+ private void SendLogAction ( LoggingEvent loggingEvent )
53+ {
54+ //we should always format event in the same thread as
55+ //many properties used in the event are associated with the current thread
56+ //like threadname, ndc stacks, threadcontent properties etc.
57+
58+ //initializing a string for the formatted log
59+ string _formattedLog = string . Empty ;
60+
61+ //if Layout is null then format the log from the Loggly Client
62+ if ( this . Layout == null )
63+ {
64+ Formatter . AppendAdditionalLoggingInformation ( Config , loggingEvent ) ;
65+ _formattedLog = Formatter . ToJson ( loggingEvent ) ;
66+ }
67+ else
68+ {
69+ _formattedLog = Formatter . ToJson ( RenderLoggingEvent ( loggingEvent ) , loggingEvent . TimeStamp ) ;
70+ }
71+
72+ //check if logMode is bulk or inputs
73+ if ( Config . LogMode == "bulk/" )
74+ {
75+ addToBulk ( _formattedLog ) ;
76+ }
77+ else if ( Config . LogMode == "inputs/" )
78+ {
79+ //sending _formattedLog to the async queue
80+ LogglyAsync . PostMessage ( _formattedLog , Config ) ;
81+ }
82+ }
83+
84+ public void addToBulk ( string log )
85+ {
86+ // store all events into a array max lenght is 100
87+ lstLogs . Add ( log . Replace ( "\n " , "" ) ) ;
88+ if ( lstLogs . Count == 100 )
89+ {
90+ SendAllEvents ( lstLogs . ToArray ( ) ) ;
91+ }
92+ }
93+
94+ private void SendAllEvents ( string [ ] events )
95+ {
96+ lstLogs . Clear ( ) ;
97+ String bulkLog = String . Join ( System . Environment . NewLine , events ) ;
98+ LogglyAsync . PostMessage ( bulkLog , Config ) ;
99+ }
55100
56101 }
57- }
102+ }
0 commit comments