@@ -32,10 +32,16 @@ public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeP
32
32
{
33
33
return ;
34
34
}
35
- LogLevel logLevel = logEntry . LogLevel ;
36
- string category = logEntry . Category ;
37
- int eventId = logEntry . EventId . Id ;
38
- Exception ? exception = logEntry . Exception ;
35
+
36
+ // We extract most of the work into a non-generic method to save code size. If this was left in the generic
37
+ // method, we'd get generic specialization for all TState parameters, but that's unnecessary.
38
+ WriteInternal ( scopeProvider , textWriter , message , logEntry . LogLevel , logEntry . Category , logEntry . EventId . Id , logEntry . Exception ,
39
+ logEntry . State != null , logEntry . State ? . ToString ( ) , logEntry . State as IReadOnlyCollection < KeyValuePair < string , object > > ) ;
40
+ }
41
+
42
+ private void WriteInternal ( IExternalScopeProvider ? scopeProvider , TextWriter textWriter , string message , LogLevel logLevel ,
43
+ string category , int eventId , Exception ? exception , bool hasState , string ? stateMessage , IReadOnlyCollection < KeyValuePair < string , object > > ? stateProperties )
44
+ {
39
45
const int DefaultBufferSize = 1024 ;
40
46
using ( var output = new PooledByteBufferWriter ( DefaultBufferSize ) )
41
47
{
@@ -48,21 +54,21 @@ public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeP
48
54
DateTimeOffset dateTimeOffset = FormatterOptions . UseUtcTimestamp ? DateTimeOffset . UtcNow : DateTimeOffset . Now ;
49
55
writer . WriteString ( "Timestamp" , dateTimeOffset . ToString ( timestampFormat ) ) ;
50
56
}
51
- writer . WriteNumber ( nameof ( logEntry . EventId ) , eventId ) ;
52
- writer . WriteString ( nameof ( logEntry . LogLevel ) , GetLogLevelString ( logLevel ) ) ;
53
- writer . WriteString ( nameof ( logEntry . Category ) , category ) ;
57
+ writer . WriteNumber ( nameof ( LogEntry < object > . EventId ) , eventId ) ;
58
+ writer . WriteString ( nameof ( LogEntry < object > . LogLevel ) , GetLogLevelString ( logLevel ) ) ;
59
+ writer . WriteString ( nameof ( LogEntry < object > . Category ) , category ) ;
54
60
writer . WriteString ( "Message" , message ) ;
55
61
56
62
if ( exception != null )
57
63
{
58
64
writer . WriteString ( nameof ( Exception ) , exception . ToString ( ) ) ;
59
65
}
60
66
61
- if ( logEntry . State != null )
67
+ if ( hasState )
62
68
{
63
- writer . WriteStartObject ( nameof ( logEntry . State ) ) ;
64
- writer . WriteString ( "Message" , logEntry . State . ToString ( ) ) ;
65
- if ( logEntry . State is IReadOnlyCollection < KeyValuePair < string , object > > stateProperties )
69
+ writer . WriteStartObject ( nameof ( LogEntry < object > . State ) ) ;
70
+ writer . WriteString ( "Message" , stateMessage ) ;
71
+ if ( stateProperties != null )
66
72
{
67
73
foreach ( KeyValuePair < string , object > item in stateProperties )
68
74
{
0 commit comments