public static ScopeProperties CaptureScopeProperties(System.Collections.IEnumerable scopePropertyCollection, ConcurrentDictionary <Type, KeyValuePair <Func <object, object>, Func <object, object> > > stateExractor) { var scope = new ScopeProperties(); var keyValueExtractor = default(KeyValuePair <Func <object, object>, Func <object, object> >); foreach (var property in scopePropertyCollection) { if (property == null) { break; } if (keyValueExtractor.Key == null) { if (!TryLookupExtractor(stateExractor, property.GetType(), out keyValueExtractor)) { break; } } AddKeyValueProperty(scope, keyValueExtractor, property); } scope.AddDispose(CreateDiagnosticLogicalContext(scopePropertyCollection)); return(scope); }
/// <summary> /// Begin a scope. Use in config with ${ndlc} /// </summary> /// <param name="state">The state (message)</param> /// <returns></returns> public IDisposable BeginScope <TState>(TState state) { if (state == null) { throw new ArgumentNullException(nameof(state)); } if (_options.CaptureMessageProperties) { if (state is IEnumerable <KeyValuePair <string, object> > messageProperties) { ScopeProperties scope = new ScopeProperties(); foreach (var property in messageProperties) { if (string.IsNullOrEmpty(property.Key)) { continue; } scope.AddProperty(property.Key, property.Value); } scope.AddDispose(NestedDiagnosticsLogicalContext.Push(state)); return(scope); } } return(NestedDiagnosticsLogicalContext.Push(state)); }
public static IDisposable CaptureScopeProperty <TState>(TState scopeProperty, ConcurrentDictionary <Type, KeyValuePair <Func <object, object>, Func <object, object> > > stateExractor) { if (!TryLookupExtractor(stateExractor, scopeProperty.GetType(), out var keyValueExtractor)) { return(CreateDiagnosticLogicalContext(scopeProperty)); } var scope = new ScopeProperties(); AddKeyValueProperty(scope, keyValueExtractor, scopeProperty); scope.AddDispose(CreateDiagnosticLogicalContext(scopeProperty)); return(scope); }
public static IDisposable CreateFromState(IEnumerable <KeyValuePair <string, object> > messageProperties) { var scope = new ScopeProperties(); //var stateString = string.Empty; foreach (var property in messageProperties.ToArray()) { if (string.IsNullOrEmpty(property.Key)) { continue; } //stateString += $"{property.Key}:{property.Value} "; scope.AddProperty(property.Key, property.Value); } scope.AddDispose(NestedDiagnosticsLogicalContext.Push(messageProperties)); return(scope); }
public static IDisposable CreateFromState <TState>(TState state, IEnumerable <KeyValuePair <string, object> > messageProperties) { ScopeProperties scope = new ScopeProperties(); foreach (var property in messageProperties) { if (String.IsNullOrEmpty(property.Key)) { continue; } scope.AddProperty(property.Key, property.Value); } scope.AddDispose(NestedDiagnosticsLogicalContext.Push(state)); return(scope); }
public static ScopeProperties CaptureScopeProperties(IReadOnlyList <KeyValuePair <string, object> > scopePropertyList) { ScopeProperties scope = new ScopeProperties(scopePropertyList.Count + 1); for (int i = 0; i < scopePropertyList.Count; ++i) { var property = scopePropertyList[i]; if (i == scopePropertyList.Count - 1 && i > 0 && property.Key == NLogLogger.OriginalFormatPropertyName) { continue; // Handle BeginScope("Hello {World}", "Earth") } scope.AddProperty(property.Key, property.Value); } scope.AddDispose(CreateDiagnosticLogicalContext(scopePropertyList)); return(scope); }