//**************************************** IDisposable ILogger.BeginScope <TState>(TState state) { var Scopes = new List <IDisposable>(); foreach (var Terminal in Terminals) { var NewScope = Terminal.BeginScope(state); if (NewScope != null) { Scopes.Add(NewScope); } } foreach (var Logger in Loggers) { var NewScope = Logger.BeginScope(state); if (NewScope != null) { Scopes.Add(NewScope); } } return(new Scope(Scopes)); }
//**************************************** void ILogEventSink.Emit(LogEvent logEvent) { // Restore any Logging or Terminal properties passed through Serilog EventId LogEventId = 0; TerminalHighlight?Highlight = null; var Indent = 0; if (logEvent.Properties.TryGetValue("EventId", out var EventIdProperty) && EventIdProperty is StructureValue StructuredEventId) { string?Name = null; int? ID = null; foreach (var Property in StructuredEventId.Properties) { if (Property.Value is not ScalarValue ScalarProperty) { continue; } switch (Property.Name) { case "Name": Name = (string?)ScalarProperty.Value; break; case "Id": ID = (int?)ScalarProperty.Value; break; } } LogEventId = new EventId(ID ?? 0, Name); } if (logEvent.Properties.TryGetValue(TerminalHighlight.ScopeProperty, out var HighlightProperty) && HighlightProperty is ScalarValue ScalarHighlight) { if (ScalarHighlight.Value is string HighlightName) { TerminalHighlight.FromName(HighlightName, out Highlight); } } // Serilog properties override based on the highest scope, so the indent value saved will be the innermost one if (logEvent.Properties.TryGetValue(TerminalIndent.ScopeProperty, out var IndentProperty) && IndentProperty is ScalarValue ScalarIndent) { if (ScalarIndent.Value is int IndentValue) { Indent = IndentValue; } } IDisposable?HighlightDisposable = null, IndentDisposable = null; // Log the entry try { if (Highlight != null) { HighlightDisposable = Terminal.BeginScope(Highlight); } if (Indent > 0) { IndentDisposable = Terminal.BeginScope(TerminalIndent.Replace(Indent)); } Terminal.Log(Translate(logEvent.Level), LogEventId, logEvent, logEvent.Exception, _MessageFormatter); } finally { IndentDisposable?.Dispose(); HighlightDisposable?.Dispose(); } }