@@ -47,9 +47,9 @@ public void Dispose()
47
47
}
48
48
}
49
49
50
- public void EnrichAndCreateScopeItem ( LogEvent logEvent , ILogEventPropertyFactory propertyFactory , out LogEventPropertyValue ? scopeItem ) => EnrichWithStateAndCreateScopeItem ( logEvent , propertyFactory , _state , out scopeItem ) ;
50
+ public void EnrichAndCreateScopeItem ( LogEvent logEvent , ILogEventPropertyFactory propertyFactory , out LogEventPropertyValue ? scopeItem ) => EnrichWithStateAndCreateScopeItem ( logEvent , propertyFactory , _state , update : false , out scopeItem ) ;
51
51
52
- public static void EnrichWithStateAndCreateScopeItem ( LogEvent logEvent , ILogEventPropertyFactory propertyFactory , object ? state , out LogEventPropertyValue ? scopeItem )
52
+ public static void EnrichWithStateAndCreateScopeItem ( LogEvent logEvent , ILogEventPropertyFactory propertyFactory , object ? state , bool update , out LogEventPropertyValue ? scopeItem )
53
53
{
54
54
if ( state == null )
55
55
{
@@ -63,8 +63,8 @@ public static void EnrichWithStateAndCreateScopeItem(LogEvent logEvent, ILogEven
63
63
// Separate handling of this case eliminates boxing of Dictionary<TKey, TValue>.Enumerator.
64
64
scopeItem = null ;
65
65
foreach ( var stateProperty in dictionary )
66
- {
67
- AddProperty ( logEvent , propertyFactory , stateProperty . Key , stateProperty . Value ) ;
66
+ {
67
+ AddProperty ( logEvent , propertyFactory , stateProperty . Key , stateProperty . Value , update ) ;
68
68
}
69
69
}
70
70
else if ( state is IEnumerable < KeyValuePair < string , object > > stateProperties )
@@ -79,21 +79,21 @@ public static void EnrichWithStateAndCreateScopeItem(LogEvent logEvent, ILogEven
79
79
}
80
80
else
81
81
{
82
- AddProperty ( logEvent , propertyFactory , stateProperty . Key , stateProperty . Value ) ;
82
+ AddProperty ( logEvent , propertyFactory , stateProperty . Key , stateProperty . Value , update ) ;
83
83
}
84
84
}
85
85
}
86
86
#if FEATURE_ITUPLE
87
87
else if ( state is System. Runtime. CompilerServices. ITuple tuple && tuple. Length == 2 && tuple[ 0 ] is string s )
88
88
{
89
89
scopeItem = null ; // Unless it's `FormattedLogValues`, these are treated as property bags rather than scope items.
90
- AddProperty ( logEvent , propertyFactory , s , tuple [ 1 ] ) ;
90
+ AddProperty ( logEvent , propertyFactory , s , tuple [ 1 ] , update ) ;
91
91
}
92
92
#else
93
93
else if ( state is ValueTuple< string , object ? > tuple )
94
94
{
95
95
scopeItem = null ;
96
- AddProperty ( logEvent , propertyFactory , tuple . Item1 , tuple . Item2 ) ;
96
+ AddProperty ( logEvent , propertyFactory , tuple . Item1 , tuple . Item2 , update ) ;
97
97
}
98
98
#endif
99
99
else
@@ -102,22 +102,29 @@ public static void EnrichWithStateAndCreateScopeItem(LogEvent logEvent, ILogEven
102
102
}
103
103
}
104
104
105
- static void AddProperty ( LogEvent logEvent , ILogEventPropertyFactory propertyFactory , string key , object ? value )
105
+ static void AddProperty ( LogEvent logEvent , ILogEventPropertyFactory propertyFactory , string key , object ? value , bool update )
106
106
{
107
- var destructureObject = false ;
107
+ var destructureObjects = false ;
108
108
109
109
if ( key . StartsWith ( '@' ) )
110
110
{
111
111
key = SerilogLogger . GetKeyWithoutFirstSymbol ( SerilogLogger . DestructureDictionary , key ) ;
112
- destructureObject = true ;
112
+ destructureObjects = true ;
113
113
}
114
114
else if ( key . StartsWith ( '$' ) )
115
115
{
116
116
key = SerilogLogger . GetKeyWithoutFirstSymbol ( SerilogLogger . StringifyDictionary , key ) ;
117
117
value = value ? . ToString ( ) ;
118
118
}
119
119
120
- var property = propertyFactory . CreateProperty ( key , value , destructureObject ) ;
121
- logEvent . AddPropertyIfAbsent ( property ) ;
120
+ var property = propertyFactory . CreateProperty ( key , value , destructureObjects ) ;
121
+ if ( update )
122
+ {
123
+ logEvent . AddOrUpdateProperty ( property ) ;
124
+ }
125
+ else
126
+ {
127
+ logEvent . AddPropertyIfAbsent ( property ) ;
128
+ }
122
129
}
123
130
}
0 commit comments