Reputation: 948
I have a C# .Net 8 Webapi, with Metrics, Tracing and Logging being sent to Azure Monitor via OpenTelemetry, and the 1.3.0-beta2 nuget package Azure.Monitor.OpenTelemetry.AspNetCore (This adds support for logging).
The configuration looks like this (in program.cs):
var serviceName = "My-import-service";
var connStr = builder.Configuration["ConnectionStrings:Azure"];
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddConsoleExporter();
});
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddAzureMonitorTraceExporter(opt => opt.ConnectionString = connStr)
.AddConsoleExporter())
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddAzureMonitorMetricExporter(opt => opt.ConnectionString = connStr)
.AddConsoleExporter())
.WithLogging(logging => logging
.AddConsoleExporter()
.AddAzureMonitorLogExporter(opt => opt.ConnectionString = connStr));
The basic information exported to Azure Monitor all appears where I'd expect it, but the extended properties being read from the Tags of the Activity we're currently in do not make it when exporting a Log entry. For example, in some middleware i add a tag:
Activity.Current?.SetTag("ProcessedTime", DateTime.UtcNow.ToString());
This appears fine in the "Request" being logged, and i can see this in Application Insights as a Custom Dimension. Such as in this image:
However, if in the controller that's then responsible for handling the request, i add a Log statement, such as:
_logger.LogInformation("Getting weather forecast");
I will see this log statement in the traces section of Application Insight, but, the tags i added at the middleware level will not be present.
If i modify the controller code to:
Activity.Current?.SetTag("SomeNewTag", "Birds live in forests");
_logger.LogInformation("Getting weather forecast");
I can see the Log entry, in the traces section, but still do not have the tags as custom dimensions, like so:
Switching over to Azure.Monitor.OpenTelemetry.Exporter 1.4.0-beta.2 as an drop-in replacement (as it appears to be newer) yields the same results.
For reference, here's how i setup program.cs:
EDIT: If you read the comment chain below, I state that i wish to add tags to a log entry, effectively, and have those show up as CustomDimensions in App Insights. The closest i've gotten to achieving this is by using a formatted string on the log entry:
_logger.LogInformation("Getting weather forecast {ProcessedTime}", DateTime.UtcNow.ToString());
However, this approach logs the string as "Getting weather forecast {ProcessedTime}", which is unwanted. Ideally, this would be logging the item as "Getting weather forecast" with the extended property "ProcessedTime". Furthermore, not being the only dev working on this, i'd like to avoid weird conventions to log items of interest.
Upvotes: 0
Views: 61
Reputation: 3473
Thanks, @MartinDotNet After considering the suggestion and as per the discussion that logs and spans are indeed separate telemetry signals, so the tags from the Activity
(span) aren't automatically propagated to the logs.
Activity
in the middleware. To handle this, Create an extension method for logging.public static void LogInfoExt(this ILogger logger, string message, object additionalData)
{
// Add the extra data (e.g., tags or state) to the log's context
logger.LogInformation("{Message} - Additional Data: {@Data}", message, additionalData);
}
Activity
tags, and in the controller, use the custom log method to include them in the log.Activity.Current?.SetTag("ProcessedTime", DateTime.UtcNow.ToString());
_log.LogInfoExt("Getting weather forecast", new { ProcessedTime = DateTime.UtcNow.ToString() });
ProcessedTime
tag gets included in the log entry and appears as a custom dimension in Application Insights, which helps with tracking and correlation.
Upvotes: 1