From 938ba42584bca35f0fab5b3c5d9908c4de99a7e4 Mon Sep 17 00:00:00 2001 From: Pascal Menuet Date: Wed, 6 Mar 2024 16:09:47 +0100 Subject: [PATCH] Add property BetterStackLogTaret.IncludeScopeContext (defaults to false) in order to be able upload the ScopeContext's properties --- .../BetterStackLogsTarget.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/BetterStack.Logs.NLog/BetterStackLogsTarget.cs b/BetterStack.Logs.NLog/BetterStackLogsTarget.cs index 723c309..45ae033 100644 --- a/BetterStack.Logs.NLog/BetterStackLogsTarget.cs +++ b/BetterStack.Logs.NLog/BetterStackLogsTarget.cs @@ -56,6 +56,11 @@ public bool CaptureSourceLocation /// public bool IncludeGlobalDiagnosticContext { get; set; } = true; + /// + /// Include Include ScopeContext's properties in logs. + /// + public bool IncludeScopeContext { get; set; } = false; + /// /// Control callsite capture of source-file and source-linenumber. /// @@ -147,6 +152,20 @@ protected override void Write(LogEventInfo logEvent) contextDictionary["gdc"] = gdcDict; } } + + if (IncludeScopeContext) + { + Dictionary scopedProperties = null; + foreach (var prop in ScopeContext.GetAllProperties()) + { + if (scopedProperties == null) + scopedProperties = new Dictionary(); + scopedProperties[prop.Key] = prop.Value; + } + if (scopedProperties != null) + contextDictionary["sc"] = scopedProperties; + } + string logMessage = RenderLogEvent(this.Layout, logEvent); var log = new Log {