.NET: Configure with log4net

New Relic offers an Apache log4net extension for New Relic Logs, allowing you link to your log data with related data across the rest of the New Relic platform. This document explains how to configure logs in context and start getting log data.

Compatibility and Requirements

To use New Relic logs in context with log4net, ensure your configuration meets the following requirements:

Access to logs in context requires a New Relic APM Pro subscription and enabling of distributed tracing. Before enabling distributed tracing, read the transition guide to learn about the effects on existing APM features and set-up recommendations.

Overview

Log4net uses appender and layout to store and format log messages. This project provides NewRelicAppender which enriches log messages with contextual information from the New Relic .NET agent if it is attached to your application. This appender will pass enriched log messages to downstream appenders for specific use case log message handle. This project also provides NewRelicLayout to output log messages in the way required by the New Relic logging endpoint. A target can then be configured to write logging data to an output folder, which can be monitored by a log forwarder to incrementally send log information to New Relic.

This diagram illustrates the flow of log messages through log4net:

Logs In Context - Log4Net
This diagram illustrates the flow of log messages through log4net, highlighting specific components of the New Relic log4net extension.

The above diagram highlights the main components of the log4net logs in context solution:

  • New Relic Appender: The NewRelicAppender adds contextual information from the .NET agent (using the API) to the log events generated by the application. This contextual information, known as linking metadata, is used by New Relic to link log messages to the transactions and spans from which they were created. This appender will pass the enriched Log Events to downstream appenders for further processing. Since the NewRelicAppender is ForwardingAppender type, it needs to be the first appender in the chain and requires another appender that can write to an actual output destination as its child in order to work.

  • New Relic Layout: The NewRelicLayout formats the enriched log events into the JSON format expected by New Relic. The appender, which this layout is assigned to, instructs log4net to output the JSON to a file in the location that the log forwarder expects.

  • New Relic Log Forwarder: The log forwarder monitors an output folder and incrementally sends New Relic formatted Log information to the New Relic Logging Endpoint. There are many log-forwarders available. For our examples, we will use Fluentd.

For more information about logging with log4net, see the Apache log4net Getting started documentation.

Configure logs in context with New Relic Logs

To configure New Relic logs in context with

  1. Enable New Relic Logs with a compatible log forwarding plugin.
  2. Install or update the .NET agent.
  3. Configure the log4net extension.
  4. Check for logging data.

Enable New Relic Logs

Confirm that you have New Relic Logs enabled, with a compatible log forwarding plugin installed to send your application logs to New Relic.

Install or update the .NET agent

Install or update to the most recent .NET agent version, and enable Distributed tracing.

Configure the log4net extension

To configure logs in context with the log4net extension, complete the following steps:

  1. Using the Visual Studio NuGet Package Manager, locate and install the NewRelic.LogEnrichers.Log4Net package.

  2. In your log4net configuration file, update your logging configuration to use the NewRelicAppender as the first level appender, and reference your existing appenders as its children. You also need to replace the layout of the appender that writes log messages to an output destination with the NewRelicLayout .

    The following log4net configuration example enriches log events with New Relic linking metadata. In addition to the existing log files, it outputs new log files in a specific JSON format at C:\logs\log4netExample.log.json for consumption by the log forwarder:

    <log4net>
      <root>
        <level value="ALL" />
        <appender-ref ref="NewRelicAppender" />
      </root>
    
      <appender name="NewRelicAppender" type="NewRelic.LogEnrichers.Log4Net.NewRelicAppender, NewRelic.LogEnrichers.Log4Net" >
        <threshold value="ALL"/>
        <appender-ref ref="FileAppender" />
      </appender>
    
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="C:\logs\log4netExample.log.json" />
        <param name="AppendToFile" value="true" />
        <layout type="NewRelic.LogEnrichers.Log4Net.NewRelicLayout, NewRelic.LogEnrichers.Log4Net">
        </layout>
      </appender>
    </log4net>
    

    Once you have configured the log4net extension and updated your logging file, you can configure your extension to send data to New Relic Logs.

    See below for an example of this configuration using the Fluentd plugin for New Relic Logs:

    <!--NewRelicLoggingExample.conf-->
    <source> 
        @type tail 
        path C:\logs\log4netExample.log.json
        pos_file C:\logs\log4netExample.log.json.pos 
        tag logfile.*
     <parse> 
        @type json 
    </parse>
    </source>
    <match **> 
        @type newrelic 
        license_key <YOUR NEW_RELIC_LICENSE_KEY>
        base_uri https://log-api.newrelic.com/log/v1
    </match>

What's next?

Now that you've set up APM logs in context, here are some potential next steps:

For more help

Recommendations for learning more: