I have a .NET project whose logs are being outputted into Elastic’s Observability platform using OpenTelemetry.
When I look at the logs on the platform (Observability > Logs > Logs Explorer), they’re only displaying the property name in curly brackets instead of the actual property value. For example, I’m seeing this:
Connection id "{ConnectionId}" stopped.
Instead of:
Connection id "0HN6DP7I45OBC" stopped.
My appsettings.json
looks like this:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"OpenTelemetry": {
"CollectorEndpoint": null,
"Headers": null
}
}
Do I need a LogLevel
node within OpenTelemetry
similar to Logging
? Or is there some other configuration somewhere else in the project?
Placeholders for structured logs, in OpenTelemetry, are stored in attributes, so the output looks like the following.
LogRecord.Body: Connection id {ConnectionId} stopped.
LogRecord.Attributes (Key:Value):
ConnectionId: 0HN6DP7I45OBC
OriginalFormat (a.k.a Body): Connection id {ConnectionId} stopped.
Here is my test steps.
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using System;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddOpenTelemetry(options =>
{
options.AddConsoleExporter();
});
// For checking the difference between OpenTelemetry and Default LogProvider
builder.Logging.AddConsole();
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapGet("/", (ILogger<Program> logger) =>
{
var connectionId = "0HN6DP7I45OBC";
// method1 work
//logger.LogInformation("Connection id "+ connectionId + " stopped.");
// method2 not work
logger.LogInformation("Connection id {ConnectionId} stopped.", connectionId);
// method3 not work
//logger.LogInformation("Connection id {0} stopped.", connectionId);
Console.WriteLine(string.Format("Jason Test Connection id {0} stopped.", connectionId));
});
app.Run();
So for this issue, I suggest you can this my method1,
logger.LogInformation("Connection id "+ connectionId + " stopped.");
it can work properly.