@@ -12,9 +12,36 @@ namespace Core.Arango.Serilog
1212{
1313 public class ArangoSerilogSink : IBatchedLogEventSink
1414 {
15+ [ Flags ]
16+ public enum LoggingRenderStrategy
17+ {
18+ RenderMessage = 1 ,
19+ StoreTemplate = 2 ,
20+ }
21+
1522 private readonly IArangoContext _arango ;
1623 private readonly string _collection ;
1724 private readonly string _database ;
25+ private readonly LoggingRenderStrategy _renderMessage ;
26+ private readonly bool _indexLevel ;
27+ private readonly bool _indexTimestamp ;
28+ private readonly bool _indexTemplate ;
29+
30+ public ArangoSerilogSink (
31+ IArangoContext arango ,
32+ string database = "logs" ,
33+ string collection = "logs" ,
34+ LoggingRenderStrategy renderMessage = LoggingRenderStrategy . RenderMessage ,
35+ bool indexLevel = false ,
36+ bool indexTimestamp = false ,
37+ bool indexTemplate = false ) : this ( arango , database , collection )
38+ {
39+ _renderMessage = renderMessage ;
40+ _indexLevel = indexLevel ;
41+ _indexTimestamp = indexTimestamp ;
42+ _indexTemplate = indexTemplate ;
43+ }
44+
1845
1946 public ArangoSerilogSink (
2047 IArangoContext arango ,
@@ -27,10 +54,14 @@ public ArangoSerilogSink(
2754
2855 try
2956 {
30- if ( ! _arango . Database . ExistAsync ( _database ) . Result )
57+ if ( ! _arango . Database . ExistAsync ( _database ) . AsTask ( ) . GetAwaiter ( ) . GetResult ( ) )
3158 _arango . Database . CreateAsync ( _database ) . AsTask ( ) . Wait ( ) ;
3259
33- if ( ! _arango . Collection . ExistAsync ( _database , collection ) . Result )
60+ var indexes = _arango . Index . ListAsync ( _database , _collection )
61+ . AsTask ( ) . GetAwaiter ( ) . GetResult ( ) ;
62+
63+ if ( ! _arango . Collection . ExistAsync ( _database , collection ) . AsTask ( ) . GetAwaiter ( ) . GetResult ( ) )
64+ {
3465 _arango . Collection . CreateAsync ( _database , new ArangoCollection
3566 {
3667 Name = _collection ,
@@ -39,6 +70,37 @@ public ArangoSerilogSink(
3970 Type = ArangoKeyType . Padded
4071 }
4172 } ) . AsTask ( ) . Wait ( ) ;
73+ }
74+
75+ if ( _indexLevel &&
76+ indexes . Any ( x => x . Name == nameof ( LogEventEntity . Level ) ) )
77+ {
78+ _arango . Index . CreateAsync ( _database , _collection , new ArangoIndex
79+ {
80+ Type = ArangoIndexType . Persistent ,
81+ Name = nameof ( LogEventEntity . Level )
82+ } ) . AsTask ( ) . Wait ( ) ;
83+ }
84+
85+ if ( _indexTimestamp &&
86+ indexes . Any ( x => x . Name == nameof ( LogEventEntity . Timestamp ) ) )
87+ {
88+ _arango . Index . CreateAsync ( _database , _collection , new ArangoIndex
89+ {
90+ Type = ArangoIndexType . Persistent ,
91+ Name = nameof ( LogEventEntity . Timestamp )
92+ } ) . AsTask ( ) . Wait ( ) ;
93+ }
94+
95+ if ( _indexTemplate &&
96+ indexes . Any ( x => x . Name == nameof ( LogEventEntity . MessageTemplate ) ) )
97+ {
98+ _arango . Index . CreateAsync ( _database , _collection , new ArangoIndex
99+ {
100+ Type = ArangoIndexType . Persistent ,
101+ Name = nameof ( LogEventEntity . MessageTemplate )
102+ } ) . AsTask ( ) . Wait ( ) ;
103+ }
42104 }
43105 catch ( Exception )
44106 {
@@ -50,13 +112,23 @@ public async Task EmitBatchAsync(IEnumerable<LogEvent> events)
50112 {
51113 try
52114 {
115+ var renderMessage = _renderMessage . HasFlag ( LoggingRenderStrategy . RenderMessage ) ;
116+ var storeTemplate = _renderMessage . HasFlag ( LoggingRenderStrategy . StoreTemplate ) ;
117+
53118 await _arango . Document . CreateManyAsync ( _database , _collection , events . Select ( x => new LogEventEntity
54119 {
55120 Level = x . Level . ToString ( ) ,
56121 Timestamp = x . Timestamp . UtcDateTime ,
57- Message = x . RenderMessage ( ) ,
122+ Message = renderMessage
123+ ? x . RenderMessage ( )
124+ : null ,
125+ MessageTemplate =
126+ storeTemplate
127+ ? x . MessageTemplate . Text
128+ : null ,
58129 Exception = x . Exception ? . ToString ( ) ,
59- Properties = x . Properties . ToDictionary ( y => y . Key ,
130+ Properties = x . Properties . ToDictionary (
131+ y => y . Key ,
60132 y => y . Value . ToString ( ) )
61133 } ) ) ;
62134 }
@@ -78,9 +150,17 @@ public class LogEventEntity
78150 public string Key { get ; set ; }
79151
80152 public DateTime Timestamp { get ; set ; }
153+
81154 public string Level { get ; set ; }
155+
156+ [ System . Text . Json . Serialization . JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingNull ) ]
82157 public string Message { get ; set ; }
158+
159+ [ System . Text . Json . Serialization . JsonIgnore ( Condition = JsonIgnoreCondition . WhenWritingNull ) ]
160+ public string MessageTemplate { get ; set ; }
161+
83162 public string Exception { get ; set ; }
163+
84164 public Dictionary < string , string > Properties { get ; set ; }
85165 }
86166 }
0 commit comments