@@ -64,69 +64,98 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
64
64
{
65
65
if ( _maskingMode == MaskingMode . Globally || SensitiveArea . Instance != null )
66
66
{
67
- var messageTemplateText = ReplaceSensitiveDataFromString ( logEvent . MessageTemplate . Text ) ;
67
+ var ( wasTemplateMasked , messageTemplateText ) = ReplaceSensitiveDataFromString ( logEvent . MessageTemplate . Text ) ;
68
68
69
- _messageTemplateBackingField . SetValue ( logEvent , Parser . Parse ( messageTemplateText ) ) ;
69
+ // Only replace the template if it was actually masked
70
+ if ( wasTemplateMasked )
71
+ {
72
+ _messageTemplateBackingField . SetValue ( logEvent , Parser . Parse ( messageTemplateText ) ) ;
73
+ }
70
74
71
75
foreach ( var property in logEvent . Properties . ToList ( ) )
72
76
{
73
- var maskedValue = MaskProperty ( property ) ;
74
-
75
- logEvent
76
- . AddOrUpdateProperty (
77
- new LogEventProperty (
78
- property . Key ,
79
- maskedValue ) ) ;
77
+ var ( wasMasked , maskedValue ) = MaskProperty ( property ) ;
78
+
79
+ // Only update the property if it was actually masked
80
+ if ( wasMasked )
81
+ {
82
+ logEvent
83
+ . AddOrUpdateProperty (
84
+ new LogEventProperty (
85
+ property . Key ,
86
+ maskedValue ) ) ;
87
+ }
80
88
}
81
89
}
82
90
}
83
91
84
- private LogEventPropertyValue MaskProperty ( KeyValuePair < string , LogEventPropertyValue > property )
92
+ private ( bool , LogEventPropertyValue ? ) MaskProperty ( KeyValuePair < string , LogEventPropertyValue > property )
85
93
{
86
94
if ( _excludeProperties . Contains ( property . Key , StringComparer . InvariantCultureIgnoreCase ) )
87
95
{
88
- return property . Value ;
96
+ return ( false , null ) ;
89
97
}
90
98
91
99
if ( _maskProperties . Contains ( property . Key , StringComparer . InvariantCultureIgnoreCase ) )
92
100
{
93
- return new ScalarValue ( _maskValue ) ;
101
+ return ( true , new ScalarValue ( _maskValue ) ) ;
94
102
}
95
103
96
- if ( property . Value is ScalarValue scalar && scalar . Value is string stringValue )
104
+ switch ( property . Value )
97
105
{
98
- return new ScalarValue ( ReplaceSensitiveDataFromString ( stringValue ) ) ;
99
- }
100
- if ( property . Value is StructureValue structureValue )
101
- {
102
- var propList = new List < LogEventProperty > ( ) ;
103
-
104
- foreach ( var prop in structureValue . Properties )
105
- {
106
- var maskedValue = MaskProperty ( new KeyValuePair < string , LogEventPropertyValue > ( prop . Name , prop . Value ) ) ;
107
-
108
- propList . Add ( new LogEventProperty ( prop . Name , maskedValue ) ) ;
109
- }
110
-
111
- return new StructureValue ( propList ) ;
106
+ case ScalarValue { Value : string stringValue } :
107
+ {
108
+ var ( wasMasked , maskedValue ) = ReplaceSensitiveDataFromString ( stringValue ) ;
109
+
110
+ if ( wasMasked )
111
+ {
112
+ return ( true , new ScalarValue ( maskedValue ) ) ;
113
+ }
114
+
115
+ return ( false , null ) ;
116
+ }
117
+ case StructureValue structureValue :
118
+ {
119
+ var propList = new List < LogEventProperty > ( ) ;
120
+ var anyMasked = false ;
121
+ foreach ( var prop in structureValue . Properties )
122
+ {
123
+ var ( wasMasked , maskedValue ) = MaskProperty ( new KeyValuePair < string , LogEventPropertyValue > ( prop . Name , prop . Value ) ) ;
124
+
125
+ if ( wasMasked )
126
+ {
127
+ anyMasked = true ;
128
+ propList . Add ( new LogEventProperty ( prop . Name , maskedValue ) ) ;
129
+ }
130
+ else
131
+ {
132
+ propList . Add ( prop ) ;
133
+ }
134
+ }
135
+
136
+ return ( anyMasked , new StructureValue ( propList ) ) ;
137
+ }
138
+ default :
139
+ return ( false , null ) ;
112
140
}
113
-
114
- return property . Value ;
115
141
}
116
142
117
- private string ReplaceSensitiveDataFromString ( string input )
143
+ private ( bool , string ) ReplaceSensitiveDataFromString ( string input )
118
144
{
145
+ var isMasked = false ;
146
+
119
147
foreach ( var maskingOperator in _maskingOperators )
120
148
{
121
149
var maskResult = maskingOperator . Mask ( input , _maskValue ) ;
122
150
123
151
if ( maskResult . Match )
124
152
{
153
+ isMasked = true ;
125
154
input = maskResult . Result ;
126
155
}
127
156
}
128
157
129
- return input ;
158
+ return ( isMasked , input ) ;
130
159
}
131
160
132
161
public static IEnumerable < IMaskingOperator > DefaultOperators => new List < IMaskingOperator >
0 commit comments