Use our Serilog 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.
To use New Relic logs in context with Serilog, 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+
- Serilog 2.5+
- Serilog File Sinks v4.0+
Serilog is a structured logging framework that records log messages from your application and creates a
LogEvent to store the message data. Using Enrichers, you can add additional information to the log events, while Sinks and Formatters allow you to format and output those log events for downstream consumption and viewing.
This diagram illustrates the flow of log messages through Serilog.
This diagram illustrates the flow of log messages through Serilog, highlighting specific components of the New Relic Serilog extension.
The above diagram highlights several components of the Serilog extension:
- New Relic Enricher: The
NewRelicEnricheradds 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.
- New Relic Formatter: The
NewRelicFormattertranslates enriched log events into the JSON format expected by New Relic. A sink instructs Serilog to output the JSON to a file in the location that the Log Forwarder expects.
- New Relic Log Forwarder: The
NewRelicFormattertranslates enriched log events into the JSON format expected by New Relic. A sink instructs Serilog to output the JSON to a file in the location that the log forwarder expects. The example below uses the New Relic Fluentd log forwarded, however there are many other log forwarders that can be used. For more information, see Introduction to log management.
For more information about Serilog log events, see the Serilog Getting started documentation.
To configure New Relic logs in context with Serilog:
- Enable log management with a compatible log forwarding plugin.
- Install or update the .NET agent.
- Configure the Serilog extension.
- Check for logging data.
To configure logs in context with the Serilog extension, complete the following steps:
In your application code, update your logging configuration to add the
The following code 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\SerilogExample.log.jsonfor consumption by the Log Forwarder:var loggerConfig = new LoggerConfiguration()loggerConfig.Enrich.WithThreadName().Enrich.WithThreadId().Enrich.WithNewRelicLogsInContext().WriteTo.File( path: @"C:\logs\ExistingLoggingOutput.txt").WriteTo.File(formatter: new NewRelicFormatter(),path: @"C:\logs\SerilogExample.log.json");var log = loggerConfig.CreateLogger();
The above configuration results in new JSON files that are written to disk. Some of these configuration options may be useful in managing the amount of disk space used and/or the performance of the sink.
Though not required, using the Serilog Asynchronous Sink Wrapper may help improve the performance by performing formatting and output of log files on a different thread.
Once you have configured the Serilog 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 tailpath C:\logs\SerilogExample.log.jsonpos_file C:\logs\SerilogExample.log.json.postag logfile.*<parse>@type json</parse></source><match **>@type newreliclicense_key <YOUR NEW_RELIC_LICENSE_KEY>base_uri https://log-api.newrelic.com/log/v1</match>
Configuration of the New Relic Logs Serilog extension may also be accomplished with file-based configuration providers.
Now that you've set up APM logs in context, here are some potential next steps: