Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions BetterStack.Logs.NLog/BetterStackLogsTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public bool CaptureSourceLocation
/// </summary>
public bool IncludeGlobalDiagnosticContext { get; set; } = true;

/// <summary>
/// Include Include ScopeContext's properties in logs.
/// </summary>
public bool IncludeScopeContext { get; set; } = false;

@snakefoot snakefoot Oct 7, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NLog TargetWithContext already provides IncludeScopeProperties that works together with GetAllProperties()


/// <summary>
/// Control callsite capture of source-file and source-linenumber.
/// </summary>
Expand Down Expand Up @@ -147,6 +152,20 @@ protected override void Write(LogEventInfo logEvent)
contextDictionary["gdc"] = gdcDict;
}
}

if (IncludeScopeContext)
{
Dictionary<string, object> scopedProperties = null;
foreach (var prop in ScopeContext.GetAllProperties())
{
if (scopedProperties == null)
scopedProperties = new Dictionary<string, object>();
scopedProperties[prop.Key] = prop.Value;
}
if (scopedProperties != null)
contextDictionary["sc"] = scopedProperties;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acronyms like "sc" are not easy for others to understand

}

string logMessage = RenderLogEvent(this.Layout, logEvent);

var log = new Log {
Expand Down