Serilog Sink that sends log events to Dynatrace https://www.dynatrace.com/
Package - Serilog.Sinks.Dynatrace | Platforms - .NET 4.5, netstandard2.0
Enable the sink and log:
var log = new LoggerConfiguration()
.WriteTo.Dynatrace(
accessToken: "xxx.yyyyyy.zzzzz",
ingestUrl: "https://{your-environment-id}.live.dynatrace.com/api/v2/logs/ingest")
.CreateLogger();
var position = new { Latitude = 25, Longitude = 134 };
var elapsedMs = 34;
log.Information("Processed {@Position} in {Elapsed:000} ms.", position, elapsedMs);
Prints to Dynatrace Log Viewer:
2021-12-30 16:41... INFO Processed { Latitude: 25, Longitude: 134 } in 034 ms.
application.id = unknown
attr.elapsed = 34
attr.position.latitude = 25
attr.position.longitude = 134
host.name = desktop-r9hnrih
Extra packages:
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Settings.Configuration
Add UseSerilog
to the Generic Host:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, logConfig) =>
logConfig.ReadFrom.Configuration(context.Configuration))
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>();
});
Add to appsettings.json
configuration:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Dynatrace" ],
"MinimumLevel": "Information",
"WriteTo": [{
"Name": "Dynatrace",
"Args": {
"accessToken": "xxx.yyyyyy.zzzzz",
"ingestUrl": "https://{your-environment-id}.live.dynatrace.com/api/v2/logs/ingest"
}
}]
}
}
Inject and log:
public class HomeController : Controller
{
private readonly ILogger _logger;
public HomeController(ILogger<HomeController> logger) { _logger = logger; }
public IActionResult Index()
{
_logger.LogInformation("Processed {@Position} in {Elapsed:000} ms.", new { Latitude = 25, Longitude = 134 }, 34);
return View();
}
}