@@ -133,21 +133,31 @@ public interface ILogTracer
133133 void Info ( string message ) ;
134134 void Warning ( string message ) ;
135135
136- ILogTracer AddTag ( string k , string v ) ;
137- ILogTracer AddTags ( ( string , string ) [ ] ? tags ) ;
136+ ILogTracer WithTag ( string k , string v ) ;
137+ ILogTracer WithTags ( ( string , string ) [ ] ? tags ) ;
138138}
139139
140- public class LogTracer : ILogTracer
140+ internal interface ILogTracerInternal : ILogTracer
141+ {
142+ void ReplaceCorrelationId ( Guid newCorrelationId ) ;
143+ void AddTags ( ( string , string ) [ ] tags ) ;
144+ }
145+
146+
147+
148+ public class LogTracer : ILogTracerInternal
141149{
142150 private string ? GetCaller ( )
143151 {
144152 return new StackTrace ( ) ? . GetFrame ( 2 ) ? . GetMethod ( ) ? . DeclaringType ? . FullName ;
145153 }
146154
155+ private Guid _correlationId ;
147156 private List < ILog > _loggers ;
157+ private Dictionary < string , string > _tags ;
148158
149- public Guid CorrelationId { get ; }
150- public IReadOnlyDictionary < string , string > Tags { get ; }
159+ public Guid CorrelationId => _correlationId ;
160+ public IReadOnlyDictionary < string , string > Tags => _tags ;
151161
152162
153163 private static List < KeyValuePair < string , string > > ConvertTags ( ( string , string ) [ ] ? tags )
@@ -172,17 +182,35 @@ private static List<KeyValuePair<string, string>> ConvertTags((string, string)[]
172182
173183 public LogTracer ( Guid correlationId , IReadOnlyDictionary < string , string > tags , List < ILog > loggers )
174184 {
175- CorrelationId = correlationId ;
176- Tags = tags ;
185+ _correlationId = correlationId ;
186+ _tags = new ( tags ) ;
177187 _loggers = loggers ;
178188 }
179189
180- public ILogTracer AddTag ( string k , string v )
190+ //Single threaded only
191+ public void ReplaceCorrelationId ( Guid newCorrelationId )
192+ {
193+ _correlationId = newCorrelationId ;
194+ }
195+
196+ //single threaded only
197+ public void AddTags ( ( string , string ) [ ] tags )
198+ {
199+ if ( tags is not null )
200+ {
201+ foreach ( var ( k , v ) in tags )
202+ {
203+ _tags [ k ] = v ;
204+ }
205+ }
206+ }
207+
208+ public ILogTracer WithTag ( string k , string v )
181209 {
182- return AddTags ( new [ ] { ( k , v ) } ) ;
210+ return WithTags ( new [ ] { ( k , v ) } ) ;
183211 }
184212
185- public ILogTracer AddTags ( ( string , string ) [ ] ? tags )
213+ public ILogTracer WithTags ( ( string , string ) [ ] ? tags )
186214 {
187215 var newTags = new Dictionary < string , string > ( Tags ) ;
188216 if ( tags is not null )
@@ -260,7 +288,7 @@ public void ForceFlush()
260288
261289public interface ILogTracerFactory
262290{
263- LogTracer MakeLogTracer ( Guid correlationId , ( string , string ) [ ] ? tags = null ) ;
291+ LogTracer CreateLogTracer ( Guid correlationId , ( string , string ) [ ] ? tags = null ) ;
264292}
265293
266294public class LogTracerFactory : ILogTracerFactory
@@ -272,7 +300,7 @@ public LogTracerFactory(List<ILog> loggers)
272300 _loggers = loggers ;
273301 }
274302
275- public LogTracer MakeLogTracer ( Guid correlationId , ( string , string ) [ ] ? tags = null )
303+ public LogTracer CreateLogTracer ( Guid correlationId , ( string , string ) [ ] ? tags = null )
276304 {
277305 return new ( correlationId , tags , _loggers ) ;
278306 }
0 commit comments