A Serilog Sink that sends log events to NewRelic Insights.
# nuget CLI
nuget install Serilog.Sinks.NewRelicInsights
# dotnet CLI
dotnet add package Serilog.Sinks.NewRelicInsights
- Create an instance of
NewRelicConfigurationOptions
"
var newRelicConfigurationOptions = new NewRelicConfigurationOptions
{
AccountId = "<your-account-key>",
ApplicationName = "ConsoleApp", // Written to NewRelic
EnvironmentName = "Development", // Written to NewRelic
EventType = "MyEvent", // Written to NewRelic
LicenseKey = "your-license-key"
};
- Next, create a logger with the NewRelicInsights Sink using the above configuration:
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.NewRelicInsights(
newRelicConfigurationOptions,
restrictedToMinimumLevel:LogEventLevel.Information)
.CreateLogger();
- Finally, set the global logger to this logger and ensure that you call Serilog.Log.CloseAndFlush() before application exits.
Log.Logger = logger;
// Before the application exits, call CloseAndFlush as the background tasks and log pipelines need to clear
Log.CloseAndFlush();
- Set up your configuration source
// Set the following in appSettings.json
{
"Serilog": {
"WriteTo": [
{
"Name": "NewRelicInsights",
"Args": {
"applicationName": "MyApp",
"environmentName": "Development",
"eventType": "MyEvent",
"accountId": "your-newrelic-account-id",
"licenseKey": "your-license-key"
}
}
]
}
}
// OR set the following environment variables: (Windows Syntax shown)
SETX Serilog__WriteTo__0__Name "NewRelicInsights" /M
SETX Serilog__WriteTo__0__Args__applicationName "ConsoleApp" /M
SETX Serilog__WriteTo__0__Args__environmentName "Development" /M
SETX Serilog__WriteTo__0__Args__eventType "MyEvent" /M
SETX Serilog__WriteTo__0__Args__accountId "your-account-id" /M
SETX Serilog__WriteTo__0__Args__licenseKey "your-license-key" /M
- Create a logger
// Requires Microsoft.Extensions.Configuration.EnvironmentVariables
var configBuilder = new ConfigurationBuilder()
.AddEnvironmentVariables();
var configuration = configBuilder.Build();
// Requires Serilog and Serilog.Settings.Configuration
var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.ReadFrom.Configuration(configuration)
.CreateLogger();
return logger;
- Finally, set the global logger to this logger and ensure that you call Serilog.Log.CloseAndFlush() before application exits.
Log.Logger = logger;
// Before the application exits, call CloseAndFlush as the background tasks and log pipelines need to clear
Log.CloseAndFlush();