Skip to content

Commit

Permalink
Updates NLog example to show enabling MDC and MDLC for NLog (#108)
Browse files Browse the repository at this point in the history
* Updates NLog example
Now enabled MDC and MDLC
Makes a call to each to add key-value pairs

* Fix broken link in README.md

Co-authored-by: Alex Hemsath <ahemsath@newrelic.com>
  • Loading branch information
jaffinito and nr-ahemsath authored Jul 9, 2021
1 parent 11d904d commit a016be5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ If the issue has been confirmed as a bug or is a Feature request, please file a
**Support Channels**

* [New Relic Documentation](https://docs.newrelic.com/docs/agents/net-agent): Comprehensive guidance for using our agent
* [New Relic Community](https://discuss.newrelic.com/tags/c/full-stack-observability/agents/.netagent): The best place to engage in troubleshooting questions
* [New Relic Community](https://discuss.newrelic.com/tags/c/full-stack-observability/agents/466/dotnetagent): The best place to engage in troubleshooting questions
* [New Relic Developer](https://developer.newrelic.com/): Resources for building a custom observability applications
* [New Relic University](https://learn.newrelic.com/): A range of online training for New Relic users of every level
* [New Relic Technical Support](https://support.newrelic.com/) 24/7/365 ticketed support. Read more about our [Technical Support Offerings](https://docs.newrelic.com/docs/licenses/license-information/general-usage-licenses/support-plan).
Expand Down
20 changes: 18 additions & 2 deletions examples/NewRelic.LogEnrichers.NLog.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private static void Main(string[] args)
// This log information will be visible in New Relic Logging. Since
// a transaction has not been started, this log message will not be
// associated to a specific transaction.
// This will not have any MDC or MDLC pairs.
_logger.Info("Hello, welcome to the Nlog Logs In Context sample app!");

do
Expand All @@ -58,11 +59,12 @@ private static void Main(string[] args)

Console.WriteLine("Press <ENTER> to continue, Q to exit.");
}
while (Console.ReadLine() != "Q");
while (Console.ReadLine().ToUpper() != "Q");

// This log information will be visible in New Relic Logging. Since
// a transaction has not been started, this log message will not be
// associated to a specific transaction.
// This will not have any MDC or MDLC pairs.
_logger.Info("Thanks for visiting, please come back soon!");
}

Expand All @@ -88,7 +90,13 @@ private static Logger CreateLogger(string folderPath_StandardLogs, string folder
// 2. Use the NewRelicJsonLayout which will write the output in the format required by NewRelic, as well
// as adding the contextual information linking transaction data (if applicable) to log events.
var newRelicFileTarget = new FileTarget("NewRelicFileTarget");
newRelicFileTarget.Layout = new NewRelicJsonLayout();
var layout = new NewRelicJsonLayout
{
IncludeMdc = true,
IncludeMdlc = true
};

newRelicFileTarget.Layout = layout;
newRelicFileTarget.FileName = Path.Combine(folderPath_NewRelicLogs, "NLogExtensions_NewRelicLogging.json");
loggerConfig.AddTarget(newRelicFileTarget);

Expand All @@ -109,6 +117,10 @@ private static Logger CreateLogger(string folderPath_StandardLogs, string folder
[MethodImpl(MethodImplOptions.NoInlining)]
private static void TestMethod(string testVal)
{
// Setup the MDC and MDLC pairs
MappedDiagnosticsContext.Set("mdcKey", "mdcValue");
MappedDiagnosticsLogicalContext.Set("mdlcKey", "mdlcValue");

_logger?.Info("Starting TestMethod - {testValue}", testVal);

try
Expand All @@ -125,6 +137,10 @@ private static void TestMethod(string testVal)
}

_logger?.Info("Ending TestMethod - {testValue}", testVal);

// Clean up after the transaction is done
MappedDiagnosticsContext.Clear();
MappedDiagnosticsLogicalContext.Clear();
}
}
}

0 comments on commit a016be5

Please sign in to comment.