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:
- Microsoft .NET Framework 4.5+ or .NET Core 2.0+
- New Relic .NET agent 8.21+
- New Relic .NET agent API 8.21+
- Log4net 2.0.8+
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:
The above diagram highlights the main components of the log4net logs in context solution:
New Relic Appender: The
NewRelicAppenderadds 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
ForwardingAppendertype, 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
NewRelicLayoutformats 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
- Enable New Relic Logs with a compatible log forwarding plugin.
- Install or update the .NET agent.
- Configure the log4net extension.
- Check for logging data.
Enable New Relic Logs
Install or update the .NET agent
Configure the log4net extension
To configure logs in context with the log4net extension, complete the following steps:
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
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.jsonfor 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>
Now that you've set up APM logs in context, here are some potential next steps:
- Explore your data using the Logs UI.
- Troubleshoot errors with distributed tracing, stack traces, application logs, and more.
- Query your data and create custom dashboards or alerts.