Skip to content

Commit

Permalink
fixes #4583
Browse files Browse the repository at this point in the history
  • Loading branch information
iammukeshm committed Oct 11, 2024
1 parent 049d6ed commit 58af19c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/api/framework/Infrastructure/Logging/Serilog/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,39 @@ public static WebApplicationBuilder ConfigureSerilog(this WebApplicationBuilder
ArgumentNullException.ThrowIfNull(builder);
builder.Host.UseSerilog((context, logger) =>
{
logger.WriteTo.OpenTelemetry(options =>
{
try
{
options.Endpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
var headers = builder.Configuration["OTEL_EXPORTER_OTLP_HEADERS"]?.Split(',') ?? [];
foreach (var header in headers)
{
var (key, value) = header.Split('=') switch
{
[string k, string v] => (k, v),
var v => throw new Exception($"Invalid header format {v}")
};

options.Headers.Add(key, value);
}
options.ResourceAttributes.Add("service.name", "apiservice");
//To remove the duplicate issue, we can use the below code to get the key and value from the configuration
var (otelResourceAttribute, otelResourceAttributeValue) = builder.Configuration["OTEL_RESOURCE_ATTRIBUTES"]?.Split('=') switch
{
[string k, string v] => (k, v),
_ => throw new Exception($"Invalid header format {builder.Configuration["OTEL_RESOURCE_ATTRIBUTES"]}")
};
options.ResourceAttributes.Add(otelResourceAttribute, otelResourceAttributeValue);
}
catch
{
//ignore
}
});
logger.ReadFrom.Configuration(context.Configuration);
logger.Enrich.FromLogContext();
logger.Enrich.WithCorrelationId();
logger
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
Expand Down

0 comments on commit 58af19c

Please sign in to comment.