66
77namespace Eleven41 . Logging
88{
9- public class LogglyLog : ILog
9+ public class LogglyLog : LogglyBase , ILog
1010 {
11- private Loggly . Logger _logger ;
12-
1311 public LogglyLog ( )
1412 {
15- string logglyKey = System . Configuration . ConfigurationManager . AppSettings [ "Loggly.Key" ] ;
16- if ( String . IsNullOrEmpty ( logglyKey ) )
17- throw new Exception ( "Loggly.Key empty or missing from appSettings." ) ;
18-
19- _logger = new Loggly . Logger ( logglyKey ) ;
20- this . Data = new Dictionary < string , object > ( ) ;
2113 }
2214
2315 public LogglyLog ( Dictionary < string , object > data )
16+ : base ( data )
2417 {
25- string logglyKey = System . Configuration . ConfigurationManager . AppSettings [ "Loggly.Key" ] ;
26- if ( String . IsNullOrEmpty ( logglyKey ) )
27- throw new Exception ( "Loggly.Key empty or missing from appSettings." ) ;
28-
29- _logger = new Loggly . Logger ( logglyKey ) ;
30- if ( data != null )
31- this . Data = new Dictionary < string , object > ( data ) ;
32- else
33- this . Data = new Dictionary < string , object > ( ) ;
3418 }
3519
36- public LogglyLog ( string logglyKey )
37- {
38- _logger = new Loggly . Logger ( logglyKey ) ;
39- this . Data = new Dictionary < string , object > ( ) ;
40- }
41-
42- public LogglyLog ( string logglyKey , Dictionary < string , object > data )
43- {
44- _logger = new Loggly . Logger ( logglyKey ) ;
45- if ( data != null )
46- this . Data = new Dictionary < string , object > ( data ) ;
47- else
48- this . Data = new Dictionary < string , object > ( ) ;
49- }
50-
51- public LogglyLog ( LogglyLog other )
52- {
53- _logger = other . _logger ;
54- this . Data = new Dictionary < string , object > ( ) ;
55- }
56-
57- public LogglyLog ( LogglyLog other , Dictionary < string , object > data )
58- {
59- _logger = other . _logger ;
60- if ( data != null )
61- this . Data = new Dictionary < string , object > ( data ) ;
62- else
63- this . Data = new Dictionary < string , object > ( ) ;
64- }
65-
66- private Dictionary < string , object > _data ;
67-
68- public Dictionary < string , object > Data
69- {
70- get { return _data ; }
71- set
72- {
73- // Ensure we this.Data is never null
74- if ( value == null )
75- throw new ArgumentNullException ( "Data" , "Data must not be null" ) ;
76-
77- _data = value ;
78- }
79- }
80-
81- public bool IsSync { get ; set ; }
82-
8320 public void Log ( LogLevels level , string sFormat , params object [ ] args )
8421 {
8522 // Call the data version
86- Log ( DateTime . UtcNow , level , null , sFormat , args ) ;
23+ Log ( this . DateTimeProvider . GetCurrentDateTime ( ) , level , null , sFormat , args ) ;
8724 }
8825
8926 public void Log ( DateTime date , LogLevels level , string sFormat , params object [ ] args )
@@ -93,12 +30,12 @@ public void Log(DateTime date, LogLevels level, string sFormat, params object[]
9330
9431 public void Log ( LogLevels level , Dictionary < string , object > messageData , string sFormat , params object [ ] args )
9532 {
96- Log ( DateTime . UtcNow , level , null , sFormat , args ) ;
33+ Log ( this . DateTimeProvider . GetCurrentDateTime ( ) , level , null , sFormat , args ) ;
9734 }
9835
9936 public void Log ( DateTime date , LogLevels level , Dictionary < string , object > messageData , string sFormat , params object [ ] args )
10037 {
101- if ( _logger == null )
38+ if ( _client == null )
10239 return ;
10340
10441 // Start with the standard properties
@@ -122,11 +59,20 @@ public void Log(DateTime date, LogLevels level, Dictionary<string, object> messa
12259 data [ "date" ] = date ;
12360
12461 // Serialize and dispatch
125- string json = Newtonsoft . Json . JsonConvert . SerializeObject ( data ) ;
62+ var logglyEvent = new Loggly . LogglyEvent ( ) ;
63+ foreach ( var key in data . Keys )
64+ {
65+ logglyEvent . Data . Add ( key , data [ key ] ) ;
66+ }
67+
12668 if ( this . IsSync )
127- _logger . LogSync ( json , true ) ;
69+ {
70+ var response = _client . Log ( logglyEvent ) . Result ;
71+ }
12872 else
129- _logger . Log ( json , true ) ;
73+ {
74+ _client . Log ( logglyEvent ) . ConfigureAwait ( false ) ;
75+ }
13076 }
13177 }
13278}
0 commit comments