I have tried a few different methods to log some custom properties to app insights using Serilog, but none of them show up in the logs. First I tried using the Enrich.FromLogContext()
method:
Program.cs:
public static IHostBuilder CreateHostBuilder() =>
new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureAppConfiguration((_, _) =>
{
})
.UseSerilog((context, _, configuration) =>
{
configuration.MinimumLevel.Override("System", LogEventLevel.Information);
configuration.MinimumLevel.Override("Microsoft", LogEventLevel.Information);
configuration.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning); // Suppressing AspNetCore framework log noise
configuration.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning); // Suppressing EntityFrameworkCore framework log noise
configuration.ReadFrom.Configuration(context.Configuration);
configuration.WriteTo.Console();
configuration.Enrich.FromLogContext();
})
.ConfigureServices((context, services) =>
{
var configuration = context.Configuration;
services.AddApplicationInsightsTelemetry(configuration);
services.AddLogging(logBuilder => logBuilder.AddSerilog(dispose: true));
}
And then adding a custom property to the log context in my class:
MyClass.cs:
private ILogger Log => Serilog.Log.ForContext(GetType());
public async Task Run()
{
using (LogContext.PushProperty("FunctionName", "MyClass.Run"))
{
Log.Here().Information("Logging from MyClass.Run()");
}
}
The property “FunctionName” did not show up along with the log message in application insights.
Next I tried explicitly adding a property to a single log message:
//logic here
Log.Here().Information("Logging from function {FunctionName}, "Run");
//logic here
But that didn’t show up either. Just to clarify: the messages are being logged, but the custom properties are not included in the customDimensions field in application insights. Am I configuring something wrong?
EDIT: I found a solution, but not quite sure why it works. If I add the following to my Serilog configuration:
configuration.WriteTo.ApplicationInsights(context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"], TelemetryConverter.Traces);
the custom dimensions are added to the logs. However, every line is also logged twice, presumably because it logs once with the WriteTo
method and once with the built in logging from services.AddApplicationInsightsTelemetry(configuration);
. What I don’t understand is why logs from AddApplicationInsightsTelemetry
doesn’t include the custom dimensions.
2
I don’t understand is why logs from
AddApplicationInsightsTelemetry
doesn’t include the custom dimensions.
To log custom dimensions using default ApplicationInsightsSDK, you need to use telemetry client.
- By default, it gives a basic custom property.
- To log any additional dimensions, you need to use Telemetry Client, which you have done later.
You can also refer my SOThread to log additional custom dimensions.
This MSDoc also explains the same
When you use the standalone package,
TelemetryClient
isn’t injected to the dependency injection . You need to create a new instance ofTelemetryClient
and use the same configuration that the logger provider uses.
As per this GitHub, you can use TelemetryClient
or create your own ITelemetryConverter
to log additional custom properties.
Custom Dimensions with TelemetryClient
using AddApplicationInsightsTelemetry