3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Diagnostics ;
6
7
using System . Diagnostics . Tracing ;
8
+ using System . Runtime . CompilerServices ;
7
9
using System . Threading ;
8
10
using Microsoft . Extensions . Primitives ;
9
11
@@ -119,19 +121,40 @@ private LoggingEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFo
119
121
/// This only gives you the human reasable formatted message.
120
122
/// </summary>
121
123
[ Event ( 1 , Keywords = Keywords . FormattedMessage , Level = EventLevel . LogAlways ) ]
122
- internal void FormattedMessage ( LogLevel Level , int FactoryID , string LoggerName , string EventId , string FormattedMessage )
124
+ internal unsafe void FormattedMessage ( LogLevel Level , int FactoryID , string LoggerName , int EventId , string EventName , string FormattedMessage )
123
125
{
124
- WriteEvent ( 1 , Level , FactoryID , LoggerName , EventId , FormattedMessage ) ;
126
+ if ( IsEnabled ( ) )
127
+ {
128
+ fixed ( char * loggerName = LoggerName )
129
+ fixed ( char * eventName = EventName )
130
+ fixed ( char * formattedMessage = FormattedMessage )
131
+ {
132
+ const int eventDataCount = 6 ;
133
+ var eventData = stackalloc EventData [ eventDataCount ] ;
134
+
135
+ SetEventData ( ref eventData [ 0 ] , ref Level ) ;
136
+ SetEventData ( ref eventData [ 1 ] , ref FactoryID ) ;
137
+ SetEventData ( ref eventData [ 2 ] , ref LoggerName , loggerName ) ;
138
+ SetEventData ( ref eventData [ 3 ] , ref EventId ) ;
139
+ SetEventData ( ref eventData [ 4 ] , ref EventName , eventName ) ;
140
+ SetEventData ( ref eventData [ 5 ] , ref FormattedMessage , formattedMessage ) ;
141
+
142
+ WriteEventCore ( 1 , eventDataCount , eventData ) ;
143
+ }
144
+ }
125
145
}
126
146
127
147
/// <summary>
128
148
/// Message() is called when ILogger.Log() is called. and the Message keyword is active
129
149
/// This gives you the logged information in a programatic format (arguments are key-value pairs)
130
150
/// </summary>
131
151
[ Event ( 2 , Keywords = Keywords . Message , Level = EventLevel . LogAlways ) ]
132
- internal void Message ( LogLevel Level , int FactoryID , string LoggerName , string EventId , ExceptionInfo Exception , IEnumerable < KeyValuePair < string , string > > Arguments )
152
+ internal void Message ( LogLevel Level , int FactoryID , string LoggerName , int EventId , string EventName , ExceptionInfo Exception , IEnumerable < KeyValuePair < string , string > > Arguments )
133
153
{
134
- WriteEvent ( 2 , Level , FactoryID , LoggerName , EventId , Exception , Arguments ) ;
154
+ if ( IsEnabled ( ) )
155
+ {
156
+ WriteEvent ( 2 , Level , FactoryID , LoggerName , EventId , EventName , Exception , Arguments ) ;
157
+ }
135
158
}
136
159
137
160
/// <summary>
@@ -140,31 +163,95 @@ internal void Message(LogLevel Level, int FactoryID, string LoggerName, string E
140
163
[ Event ( 3 , Keywords = Keywords . Message | Keywords . FormattedMessage , Level = EventLevel . LogAlways , ActivityOptions = EventActivityOptions . Recursive ) ]
141
164
internal void ActivityStart ( int ID , int FactoryID , string LoggerName , IEnumerable < KeyValuePair < string , string > > Arguments )
142
165
{
143
- WriteEvent ( 3 , ID , FactoryID , LoggerName , Arguments ) ;
166
+ if ( IsEnabled ( ) )
167
+ {
168
+ WriteEvent ( 3 , ID , FactoryID , LoggerName , Arguments ) ;
169
+ }
144
170
}
145
171
146
172
[ Event ( 4 , Keywords = Keywords . Message | Keywords . FormattedMessage , Level = EventLevel . LogAlways ) ]
147
- internal void ActivityStop ( int ID , int FactoryID , string LoggerName )
173
+ internal unsafe void ActivityStop ( int ID , int FactoryID , string LoggerName )
148
174
{
149
- WriteEvent ( 4 , ID , FactoryID , LoggerName ) ;
175
+ if ( IsEnabled ( ) )
176
+ {
177
+ fixed ( char * loggerName = LoggerName )
178
+ {
179
+ const int eventDataCount = 3 ;
180
+ var eventData = stackalloc EventData [ eventDataCount ] ;
181
+
182
+ SetEventData ( ref eventData [ 0 ] , ref ID ) ;
183
+ SetEventData ( ref eventData [ 1 ] , ref FactoryID ) ;
184
+ SetEventData ( ref eventData [ 2 ] , ref LoggerName , loggerName ) ;
185
+
186
+ WriteEventCore ( 4 , eventDataCount , eventData ) ;
187
+ }
188
+ }
150
189
}
151
190
152
191
[ Event ( 5 , Keywords = Keywords . JsonMessage , Level = EventLevel . LogAlways ) ]
153
- internal void MessageJson ( LogLevel Level , int FactoryID , string LoggerName , string EventId , string ExceptionJson , string ArgumentsJson )
192
+ internal unsafe void MessageJson ( LogLevel Level , int FactoryID , string LoggerName , int EventId , string EventName , string ExceptionJson , string ArgumentsJson )
154
193
{
155
- WriteEvent ( 5 , Level , FactoryID , LoggerName , EventId , ExceptionJson , ArgumentsJson ) ;
194
+ if ( IsEnabled ( ) )
195
+ {
196
+ fixed ( char * loggerName = LoggerName )
197
+ fixed ( char * eventName = EventName )
198
+ fixed ( char * exceptionJson = ExceptionJson )
199
+ fixed ( char * argumentsJson = ArgumentsJson )
200
+ {
201
+ const int eventDataCount = 7 ;
202
+ var eventData = stackalloc EventData [ eventDataCount ] ;
203
+
204
+ SetEventData ( ref eventData [ 0 ] , ref Level ) ;
205
+ SetEventData ( ref eventData [ 1 ] , ref FactoryID ) ;
206
+ SetEventData ( ref eventData [ 2 ] , ref LoggerName , loggerName ) ;
207
+ SetEventData ( ref eventData [ 3 ] , ref EventId ) ;
208
+ SetEventData ( ref eventData [ 4 ] , ref EventName , eventName ) ;
209
+ SetEventData ( ref eventData [ 5 ] , ref ExceptionJson , exceptionJson ) ;
210
+ SetEventData ( ref eventData [ 6 ] , ref ArgumentsJson , argumentsJson ) ;
211
+
212
+ WriteEventCore ( 5 , eventDataCount , eventData ) ;
213
+ }
214
+ }
156
215
}
157
216
158
217
[ Event ( 6 , Keywords = Keywords . JsonMessage | Keywords . FormattedMessage , Level = EventLevel . LogAlways , ActivityOptions = EventActivityOptions . Recursive ) ]
159
- internal void ActivityJsonStart ( int ID , int FactoryID , string LoggerName , string ArgumentsJson )
218
+ internal unsafe void ActivityJsonStart ( int ID , int FactoryID , string LoggerName , string ArgumentsJson )
160
219
{
161
- WriteEvent ( 6 , ID , FactoryID , LoggerName , ArgumentsJson ) ;
220
+ if ( IsEnabled ( ) )
221
+ {
222
+ fixed ( char * loggerName = LoggerName )
223
+ fixed ( char * argumentsJson = ArgumentsJson )
224
+ {
225
+ const int eventDataCount = 4 ;
226
+ var eventData = stackalloc EventData [ eventDataCount ] ;
227
+
228
+ SetEventData ( ref eventData [ 0 ] , ref ID ) ;
229
+ SetEventData ( ref eventData [ 1 ] , ref FactoryID ) ;
230
+ SetEventData ( ref eventData [ 2 ] , ref LoggerName , loggerName ) ;
231
+ SetEventData ( ref eventData [ 3 ] , ref ArgumentsJson , argumentsJson ) ;
232
+
233
+ WriteEventCore ( 6 , eventDataCount , eventData ) ;
234
+ }
235
+ }
162
236
}
163
237
164
238
[ Event ( 7 , Keywords = Keywords . JsonMessage | Keywords . FormattedMessage , Level = EventLevel . LogAlways ) ]
165
- internal void ActivityJsonStop ( int ID , int FactoryID , string LoggerName )
239
+ internal unsafe void ActivityJsonStop ( int ID , int FactoryID , string LoggerName )
166
240
{
167
- WriteEvent ( 7 , ID , FactoryID , LoggerName ) ;
241
+ if ( IsEnabled ( ) )
242
+ {
243
+ fixed ( char * loggerName = LoggerName )
244
+ {
245
+ const int eventDataCount = 3 ;
246
+ var eventData = stackalloc EventData [ eventDataCount ] ;
247
+
248
+ SetEventData ( ref eventData [ 0 ] , ref ID ) ;
249
+ SetEventData ( ref eventData [ 1 ] , ref FactoryID ) ;
250
+ SetEventData ( ref eventData [ 2 ] , ref LoggerName , loggerName ) ;
251
+
252
+ WriteEventCore ( 7 , eventDataCount , eventData ) ;
253
+ }
254
+ }
168
255
}
169
256
170
257
/// <inheritdoc />
@@ -355,5 +442,37 @@ internal LoggerFilterRule[] GetFilterRules()
355
442
{
356
443
return _filterSpec ;
357
444
}
445
+
446
+ [ NonEvent ]
447
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
448
+ private static unsafe void SetEventData < T > ( ref EventData eventData , ref T value , void * pinnedString = null )
449
+ {
450
+ if ( typeof ( T ) == typeof ( string ) )
451
+ {
452
+ var str = value as string ;
453
+ #if DEBUG
454
+ fixed ( char * rePinnedString = str )
455
+ {
456
+ Debug . Assert ( pinnedString == rePinnedString ) ;
457
+ }
458
+ #endif
459
+
460
+ if ( pinnedString != null )
461
+ {
462
+ eventData . DataPointer = ( IntPtr ) pinnedString ;
463
+ eventData . Size = checked ( ( str . Length + 1 ) * sizeof ( char ) ) ; // size is specified in bytes, including null wide char
464
+ }
465
+ else
466
+ {
467
+ eventData . DataPointer = IntPtr . Zero ;
468
+ eventData . Size = 0 ;
469
+ }
470
+ }
471
+ else
472
+ {
473
+ eventData . DataPointer = ( IntPtr ) Unsafe . AsPointer ( ref value ) ;
474
+ eventData . Size = Unsafe . SizeOf < T > ( ) ;
475
+ }
476
+ }
358
477
}
359
478
}
0 commit comments