• /
  • Log in
  • Free account

.NET: Configure with log4net

Use the Apache log4net extension to link to your log data with related data across the rest of the New Relic platform. Read on to learn how to configure logs in context and enrich your log data.

Compatibility and requirements

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


Log4net uses appender and layout to store and format log messages. NewRelicAppender enriches log messages with contextual information from the New Relic .NET agent if it is attached to your application. The appender passes enriched log messages to downstream appenders for specific use case log message handle.

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

    <level value="ALL" />
    <appender-ref ref="NewRelicAppender" />
    <appender name="NewRelicAppender" type="NewRelic.LogEnrichers.Log4Net.NewRelicAppender, NewRelic.LogEnrichers.Log4Net" >
    <threshold value="ALL"/>
    <appender-ref ref="FileAppender" />
    <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">

    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:

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

What's next?

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

For more help

If you need more help, check out these support and learning resources:

Create issueEdit page
Copyright © 2021 New Relic Inc.