Skip to content

Include original message in NLog output when local decoration is enabled #1249

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

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/Agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes
* Resolves an issue where some instrumentation was missing for Microsoft.Data.SqlClient in .NET Framework. [#1248](https://github.com/newrelic/newrelic-dotnet-agent/pull/1248)
* Resolves an issue with local log decoration for NLog where the original log message was not included in the output. [#1249](https://github.com/newrelic/newrelic-dotnet-agent/pull/1249)

## [10.1.0] - 2022-09-12

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void DecorateLogMessage(object logEvent, Type logEventType, IAgent agent

// this cannot be made a static since it is unique to each logEvent
var messageSetter = VisibilityBypasser.Instance.GeneratePropertySetter<string>(logEvent, "Message");
messageSetter(messageGetter + " " + formattedMetadata);
messageSetter(originalMessage + " " + formattedMetadata);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using MultiFunctionApplicationHelpers;
Expand All @@ -24,6 +26,7 @@ public abstract class LocalDecorationTestsBase<TFixture> : NewRelicIntegrationTe
private const string _primaryApplicationName = "Local Decoration Test App Name";
private const string _secondaryApplicationName = "Some other testing application name";
private const string _compositeApplicationName = _primaryApplicationName + ", " + _secondaryApplicationName;
private const string _testMessage = "DecorateMe";

public LocalDecorationTestsBase(TFixture fixture, ITestOutputHelper output, bool decorationEnabled, LayoutType layoutType, LoggingFramework loggingFramework) : base(fixture)
{
Expand All @@ -34,7 +37,7 @@ public LocalDecorationTestsBase(TFixture fixture, ITestOutputHelper output, bool

_fixture.AddCommand($"LoggingTester SetFramework {loggingFramework}");
_fixture.AddCommand($"LoggingTester Configure{layoutType}LayoutAppenderForDecoration");
_fixture.AddCommand($"LoggingTester CreateSingleLogMessageInTransaction DecorateMe DEBUG");
_fixture.AddCommand($"LoggingTester CreateSingleLogMessageInTransaction {_testMessage} DEBUG");

_fixture.RemoteApplication.AppName = _compositeApplicationName;

Expand All @@ -57,12 +60,18 @@ public LocalDecorationTestsBase(TFixture fixture, ITestOutputHelper output, bool
[Fact]
public void LogIsDecorated()
{
var testOutput = _fixture.RemoteApplication.CapturedOutput.StandardOutput;
// Make sure the original message is there
var commandResults = Regex.Split(testOutput, System.Environment.NewLine).Where(l => !l.Contains("EXECUTING"));
Assert.Contains(_testMessage, string.Join(System.Environment.NewLine, commandResults));

// Sample decorated data we are looking for:
// "NR-LINKING|MjczMDcwfEFQTXxBUFBMSUNBVElPTnwxODQyMg|blah.hsd1.ca.comcast.net|45f120972d61834b96fb890d2a8f97e7|840d9a82e8bc18a8|myApplicationName|"
var regex = new Regex(@"NR-LINKING\|([a-zA-Z0-9]*)\|([a-zA-Z0-9._-]*)\|([a-zA-Z0-9]*)\|([a-zA-Z0-9]*)\|(.+?)\|");
if (_decorationEnabled)
{
var match = regex.Match(_fixture.RemoteApplication.CapturedOutput.StandardOutput);
// Make sure the added metadata is there
var match = regex.Match(testOutput);
Assert.True(match.Success);
Assert.NotEmpty(match.Groups);
var entityGuid = match.Groups[1].Value;
Expand All @@ -76,6 +85,7 @@ public void LogIsDecorated()
Assert.NotNull(traceId);
Assert.NotNull(spanId);
Assert.Equal(_primaryApplicationName, entityName);

}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public void ConfigureJsonLayoutAppenderForDecoration()
{
#if NETCOREAPP2_2_OR_GREATER || NET471_OR_GREATER // Only supported in newer versions of .NET
SerializedLayout serializedLayout = new SerializedLayout();
#if NETFRAMEWORK
serializedLayout.AddMember("Message");
#else
serializedLayout.AddMember("message");
#endif
serializedLayout.AddMember("NR_LINKING");
serializedLayout.ActivateOptions();

Expand Down