public ActionContext( [CallerMemberName] string name = null, ActionContextSettings settings = null, IEnumerable <ISanitizer> logSanitizers = null) : this("default", name, settings, logSanitizers) { }
public ActionContext( string contextGroupName = "default", [CallerMemberName] string name = null, ActionContextSettings settings = null, IEnumerable <ISanitizer> logSanitizers = null) { _stopwatch = new Stopwatch(); _stopwatch.Start(); _parent = _namedContexts.GetOrAdd(contextGroupName, new AsyncLocal <ActionContext>()).Value; _namedContexts[contextGroupName].Value = this; ContextName = name; ContextGroupName = contextGroupName; if (IsRoot) { Settings = settings ?? new ActionContextSettings(); Depth = 0; State = new ContextState(logSanitizers); Logger = new ContextLogger(this); } else { Settings = _parent.Settings; Depth = _parent.Depth + 1; State = _parent.State; Logger = _parent.Logger; Logger.TrySetContext(this); } var shouldSuppressStartMessage = !IsRoot && Settings.SuppressChildContextStartMessages; var shouldAlwaysShowStart = Settings.EnableContextStartMessage && !shouldSuppressStartMessage; Logger.Log(Settings.ContextStartMessageLevel, $"Context {ContextName} has started.", !shouldAlwaysShowStart); Loaded?.Invoke(this); }
public ActionContext( string contextGroupName = "default", [CallerMemberName] string name = null, ActionContextSettings settings = null, IEnumerable <ISanitizer> logSanitizers = null) { _logSanitizers = logSanitizers?.ToList(); _stopwatch = new Stopwatch(); _stopwatch.Start(); _asyncLocalStacks.Value ??= new ConcurrentDictionary <string, ActionContextStack>(); _namedStacks = _asyncLocalStacks.Value; _stack = _namedStacks.GetOrAdd(contextGroupName, new ActionContextStack()); _parent = _stack.Peek(); _stack.Push(this); var id = Guid.NewGuid(); var causationId = _parent?.Info.Id ?? id; var correlationId = _stack.CorrelationId; if (_parent == null) { Settings = settings ?? new ActionContextSettings(); Info = new ContextInfo( true, 0, name, contextGroupName, _stack.CorrelationId); State = new ContextState(_logSanitizers); Logger = new ContextLogger(this); } else { Settings = _parent.Settings; Info = new ContextInfo( false, _parent.Info.Depth + 1, name, contextGroupName, _stack.CorrelationId, _parent?.Info?.Id); State = _parent.State; Logger = _parent.Logger; Logger.TrySetContext(this); } var entryType = Info.IsRoot ? ContextLogEntryType.ContextStart : ContextLogEntryType.ChildContextStart; Logger.LogAsType(Settings.ContextStartMessageLevel, $"Context {Info.ContextName} has started.", entryType); Loaded?.Invoke(this); }