An NLog target that sends logs to New Relic Logs
Note: This is an unofficial package. Since this is an experimental library, please consider using more robust log shipping method (e.g. fluentd) in the production.
- New Relic Logs subscription (License Key or Insert API key is required)
- New Relic APM subscription if you'd like to enable Logs in Context
- NLog 4.6.0 or above and NewRelic.LogEnrichers.NLog 1.0.0 or above. (These are dependent libraries.)
-
Add NLog.Targets.NewRelicLab.Logs package.
-
Configure NLog Target with your favorite way. Note that this Targtet always uses
NewRelicJsonLayout
. You should not specify other targets. You can configure AsyncTaskTarget properties, sinceNewRelicLogsTarget
inheritsAsyncTaskTarget
.Here are configuration examples. You can configure other options as long as NLog supports.
- From code
using NLog; using NLog.Config; using NLog.Targets.NewRelicLab.Logs;
var loggerConfig = new LoggingConfiguration(); var newRelicTarget = new NewRelicLogsTarget() { LicenseKey = "<REPLACE_YOUR_LICENSE_KEY>", Endpoint = "https://log-api.newrelic.com/log/v1", //default Endpoint is US EscapeJson = true, // If you log message is formatted as JSON, you can forcely formate the JSON to plain formatted string. // You can specify all properties of AsyncLogTarget BatchSize = 10, RetryCount = 3 // you don't have to specify Layout. We always use `NewRelicJsonLayout` whatever layout you specify. }; loggerConfig.AddRuleForAllLevels(newRelicTarget); loggerConfig.AddRuleForAllLevels(new ConsoleTarget()); LogManager.Configuration = loggerConfig;
- With NLog.config
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <extensions> <add assembly="NLog.Targets.NewRelicLab.Logs"/> </extensions> <targets> <target name="logNewRelic" xsi:type="NewRelicLogs" endpoint="https://log-api.newrelic.com/log/v1" licenseKey="REPLACE_YOUR_KEY" escapeJson="false" batchSize="100" retryCount="3" taskDelayMilliseconds="1000" /> <target name="logconsole" xsi:type="Console" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="logconsole" /> <logger name="*" minlevel="Debug" writeTo="logNewRelic" /> </rules> </nlog>
-
(Option) Instead of specify LicenseKey in the code or configuration file, you can specify one of the following ENVIRONMENT VARIABLEs to start the process:
NEW_RELIC_LICENSE_KEY
is for a New Relic License Key, orNEW_RELIC_INSERT_KEY
is for an Insert API key. -
Output your log with NLog.
var logger = LogManager.GetLogger("Example");
for (int i = 0; i < 100; i++)
{
logger.Info($"Hello New Relic Logs {i}");
}
- You will see your log in New Relic Logs.
- Bumps System.Text.Encodings.Web from 5.0.0 to 5.0.1.
- more InternalLog
- refactor GZipZStringContent class
- support force escape JSON
- add ConfigureAwait(false) to async invocation
- add NLog.config example
Experimental Release.
Please enable NLog Internal Logging and submit issue with your environment, configuration and logs.