Reputation: 9477
Is it possible to use Serilog with OpenTelemetry?
https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/logs/getting-started/README.md
Upvotes: 17
Views: 22085
Reputation: 891
Personally I made it work without using serilog-sinks-opentelemetry but directly using "writeToProviders: true" in the AddSerilog extension method
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Services.AddSerilog(
loggerConfiguration => /* configure serilog ! */,
true, writeToProviders: true);
because by default, Serilog ignores providers, since there are usually equivalent Serilog sinks available, and these work more efficiently with Serilog's pipeline. If provider support is needed, it can be optionally enabled
Afterwards, just use open telemetry package (OpenTelemetry.Extensions.Hosting) to activate the provider in your aspnet app:
// put logs into Open telemetry
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeScopes = true;
logging.IncludeFormattedMessage = true;
// optional if you use default export values Url
var logsExporter = builder.Configuration.GetSection("OpenTelemetry:Logs");
logging.AddOtlpExporter(opts => { logsExporter.Bind(opts); });
});
Upvotes: 8
Reputation: 9477
A Serilog sink for opentelemetry has been created and can be found at this GitHub repository: https://github.com/serilog/serilog-sinks-opentelemetry.
Upvotes: 3
Reputation: 31867
Yes! It's now possible using Serilog.Sinks.OpenTelemetry:
// dotnet add package serilog.sinks.opentelemetry --prerelease
Log.Logger = new LoggerConfiguration()
.WriteTo.OpenTelemetry(endpoint: "http://127.0.0.1:4317")
.CreateLogger();
Upvotes: 10
Reputation: 4210
One approach you might consider if you're running apps in Docker or Kubernetes is to just make Serilog log to console (as per 12 factor logging guidelines). From there the console logs can get received by a collector/forwarder agent such as the OpenTelemetry collector agent (or others like Logstash of Fluentd) and exported to a destination solution (e.g. Splunk or Elasticsearch). Some ways for a collector/forwarder agent to receive console logs include:
A lot of the collector/forwarder agents like the ones mentioned have the ability to transform and enrich log messages on their way through e.g. you could reshape log messages to the Open Telemetry log data model described here.
Actually I've played around with the OpenTelemetry collector agent and it does some out of the box transformations e.g. a log message of "test" received by the OpenTelemetry collector agent via the filelog receiver ends up looking like the following after it gets exported (that is, without any processing/transformation on the way through):
{
"time_unix_nano": 1637538007545231018,
"body": {
"Value": {
"string_value": "test"
}
},
"attributes": [
{
"key": "file.name",
"value": {
"Value": {
"string_value": "filename.log"
}
}
}
],
"trace_id": "",
"span_id": ""
}
I suppose if you were to log as JSON in Serilog and enrich the logs with trace & span IDs using a Serilog enricher similar to this one (disclaimer this is a plugin I wrote a few years back when things were still known as OpenTracing), you could potentially do some transformation in the collector/forwarder agent to get the trace & span IDs populated correctly as per the defined OTEL log data model.
If you don't want to do that approach outlined above (which might be overly complex for simple scenarios) you could do some custom output formatting - see here for formatting output in Serilog, and send the logs directly to your sink...
Upvotes: 6
Reputation: 2139
Maybe what you may be looking for is a way to reuse your Serilog code to send data to an OpenTelemetry compatible endpoint (aka. Sink). https://www.nuget.org/packages/Serilog.OpenTelemetry/
Another way of keeping your code as is, just add an exporter to #otel. This thread explains the details: https://github.com/open-telemetry/opentelemetry-dotnet/issues/1955
Upvotes: -2