Net Agent Troubleshooting Framework Logs in Context

Log4net

General

  • Review the Compatibility and Requirements for setting up Logs in Context for the .Net agent and Log4net:
  • Ensure you have distributed tracing enabled for the .Net agent.
  • Make sure you have used the Visual Studio NuGet Package Manager to locate and install the NewRelic.LogEnrichers.Log4net package.
  • Double check the logging configuration in your application code and make sure you’ve used NewRelicAppender and NewRelicLayout correctly.
    • We strongly recommend looking at step two under Configure the Log4net extension in our documentation for a code example.
    • From Apache’s docs: “Log4net has three main components: loggers, appenders and layouts…These components are helped by filters that control the actions of the appender and object renderers that turn objects into strings.”
    • Ensure that the NewRelicAppender is the first level appender in your configuration and references your existing appenders as its children.
      • This means that you first need to reference the NewRelicAppender in the Root section of your configuration. From Apache’s docs:
        • “Only one root logger element may only be defined and it must be a child of element. The root logger is the root of the logger hierarchy. All loggers ultimately inherit from this logger.”
      • Then you need to create an Appender section called NewRelicAppender that references the log enricher package in its “type” parameter:
        • type=“NewRelic.LogEnrichers.Log4Net.NewRelicAppender, NewRelic.LogEnrichers.Log4Net”
        • You will also need to make sure you use a “appender-ref” to allow the new relic appender to reference your existing appenders.
    • Once your Appenders are correctly referenced, double check that you have set NewRelicLayout in the Layout element for the Appender that writes your log messages to an output destination:
      • For example, you would add the above to FileAppender if you are using that.
  • Once you have configured the enricher it is a good idea to examine your logs to check that they are formatted as JSON and contain the following fields:
    • “entity.name”
      • Should be the application name set in APM for the service you’ve configured Logs in Context for.
    • “entity.type”
      • Should be the string “SERVICE”
    • “entity.guid”
      • The ID of the entity.
    • “trace.id"
      • Will only be present if distributed tracing is enabled and a corresponding transaction generates a log.
    • "span.id”
      • Will only be present if distributed tracing is enabled and a corresponding span generates a log.
    • “hostname”
      • This should be the hostname gathered by the APM agent.

Troubleshooting

  • Make sure your APM language agent is installed and set up correctly.

  • Make sure that you have Distributed Tracing configured correctly for your application. If you do not you will not see a span.id and trace.id in your log metadata.

  • If you aren’t seeing logs in the Logs UI. Ensure that you have installed and configured a New Relic plugin for a compatible log forwarder:

  • Check your Logs UI in New Relic and query using the the following:

    • has: span.id has:trace.id
  • Check your configuration against our examples on Github:

  • Using Log4net’s internal debug logs can sometimes be helpful. You can generate debug logs by adding the following to the top of your application’s configuration file (eg: web.config) inside of the element (please note I’ve include the element to show correct placement but it should already exist in your config):

<system.diagnostics>

<add

name=“textWriterTraceListener”

type=“System.Diagnostics.TextWriterTraceListener”

initializeData=“C:\tmp\log4net.txt” />

</system.diagnostics>

NLog

General

  • Review the Compatibility and Requirements for setting up Logs in Context for the .Net agent and NLog:
  • Ensure you have distributed tracing enabled for the .Net agent.
  • Make sure you have used the Visual Studio NuGet Package Manager to locate and install the NewRelic.LogEnrichers.NLog package.
  • Double check the logging configuration in your application code and make sure you’ve used NewRelicJsonLayout correctly.
    • NewRelicJsonLayout reformats your logs to JSON and adds New Relic’s metadata which provides the context for Logs in Context.
    • Since NLog uses “Targets” to define how a log is written it is a good idea to double check their valid options. NLog has a list of them in their documentation under Configuration Options. Common ones are:
    • Look at step two under Configure the NLog extension in our documentation for a code example.
  • Alternatively you can also configure NLog by using a file based configuration. If you are doing so please pay special attention to the requirements and examples shown in our docs here:
  • Once you have configured the enricher it is a good idea to examine your logs to check that they are formatted as JSON and contain the following fields:
    • “entity.name”
      • Should be the application name set in APM for the service you’ve configured Logs in Context for.
    • “entity.type”
      • Should be the string “SERVICE”
    • “entity.guid”
      • The ID of the entity.
    • “trace.id"
      • Will only be present if distributed tracing is enabled and a corresponding transaction generates a log.
    • "span.id”
      • Will only be present if distributed tracing is enabled and a corresponding span generates a log.
    • “hostname”
      • This should be the hostname gathered by the APM agent.

Troubleshooting

Serilog

General

formatter: new NewRelicFormatter(),

path: @“C:\logs\SerilogExample.log.json”);

  • Serilog provides many sinks for their WriteTo configuration object. You can use these to write log events to storage in various formats. The list of available sinks is found here:

  • Look at step two under Configure the Serilog extension in our documentation for a code example that uses the Serilog File Sink.

  • Alternatively you can also configure Serilog by using a file based configuration. If you are doing so please pay special attention to the requirements and examples shown in our docs here:

  • Once you have configured the enricher it is a good idea to examine your logs to check that they are formatted as JSON and contain the following fields:

    • “entity.name”
      • Should be the application name set in APM for the service you’ve configured Logs in Context for.
    • “entity.type”
      • Should be the string “SERVICE”
    • “entity.guid”
      • The ID of the entity.
    • “trace.id"
      • Will only be present if distributed tracing is enabled and a corresponding transaction generates a log.
    • "span.id”
      • Will only be present if distributed tracing is enabled and a corresponding span generates a log.
    • “hostname”
      • This should be the hostname gathered by the APM agent.

Troubleshooting

  • Make sure your APM language agent is installed and set up correctly.

  • Make sure that you have Distributed Tracing configured correctly for your application. If you do not you will not see a span.id and trace.id in your log metadata.

  • If you aren’t seeing logs in the Logs UI. Ensure that you have installed and configured a New Relic plugin for a compatible log forwarder:

  • Check your Logs UI in New Relic and query using the the following:

    • has: span.id has:trace.id
  • Check your configuration against our examples on Github:

  • Serilog has a “selflog” option that you may be helpful for debugging:

2 Likes