I have a following OpenTelemetry configuration in my ASP.NET Core 8 code (it pushes logs and metrics to Application Insights on Azure):
builder.Services
.AddOpenTelemetry()
.UseAzureMonitor(x => x.ConnectionString = configuration.AppIns.ConnStr)
.ConfigureResource(builder =>
{
builder.Clear();
builder
.AddService(serviceName: "MyService1", serviceInstanceId: Environment.MachineName);
})
.WithMetrics(builder => {})
.WithTracing();
And in my classes I use logs like that:
public class MyClass1
{
private readonly ILogger<MyClass1> _logger;
public MyClass1(ILogger<MyClass1> logger)
{
// ...
void Method()
{
_logger.LogInformation("test1");
// ...
}
}
}
The problem is that in logs I don't see the class name (with namespace), nor the file path where the log was logged. In other words, how to see MyClass1
in Application Insights CustomDimensions.
Since version 1.4.0-beta.1
it's possible to log the class name that has thrown an exception or create log with telemetry exporter.
Add to your csproj
:
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.4.0-beta.1" />
Until this feature comes into the official version, invoke in your app:
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);
Tracing should be configured more or less like this:
.WithTracing(cfg => { cfg
.AddAspNetCoreInstrumentation(opt => opt.RecordException = true)
.AddSource("Azure.*")
.AddAzureMonitorTraceExporter(opt => opt.ConnectionString = azureMonitorConnectionString);
});
As a result, you will see CategoryName
field under CustomDimensions
in App Insights. The field will contain the name of the class that originated the log.
More info: https://github.com/Azure/azure-sdk-for-net/pull/44754