Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property BetterStackLogsTarget.IncludeScopeContext #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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;
Copy link
Contributor

@snakefoot snakefoot Oct 7, 2024

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
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