serilog-sinks-applicationinsights
serilog-sinks-applicationinsights copied to clipboard
Duplicate Logs Using Serilog Logger in Azure Function
I need to use serilog to send message with custom properties to application insights on Azure Functions.
I'm using the code below in my startup and I'm getting duplicate logs in application insights. One log entry containing the custom properties that I've pushed to Serilog and the other doesn't
public override void Configure(IFunctionsHostBuilder builder) { var serilogLogger = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo .ApplicationInsights(TelemetryConfiguration.CreateDefault(), TelemetryConverter.Traces) .CreateLogger(); builder .Services .AddHttpClient() .AddLogging(l => l.AddSerilog(serilogLogger)) .AddSingleton(serilogLogger); }
Any thoughts on what might be going wrong here?
Note: I tried the following too:
builder
.Services
.AddHttpClient()
.AddLogging(l =>
{
l.ClearProviders().AddSerilog(serilogLogger)
})
.AddSingleton(serilogLogger);
And this fixed the "duplicate" issue but then I would have a log that contains my custom properties but is missing some key properties (e.g. operation id, cloud role name, etc)
Any update on this?
any other solution? I have this same problem in the database
I'm also experiencing this.
Same issue here. Any solution or workaround available?
Ditto! I have same issue.
Here's my code. Using telemetryConfiguration since Microsoft has decided to drop support for using InstrumentationKey only. They will require the full Application Insights ConnectionString. We found that they already do require it in Gov Azure.
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
\\ ...
var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Is(logEventLevel)
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.WithProperty("ApplicationName", "OurAppName")
.WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces, logEventLevel);
Log.Logger = loggerConfiguration.CreateLogger();
services.AddLogging(configure => configure.AddSerilog(dispose: true));
Note: In .net core/5/6 apps, TelemetryConfiguration will be auto populated with the App Insights connection string from your env var or appsettings.json if exists.
Hi! We're currently switching maintenance teams and low on bandwidth; if anyone is able to help out by investigating this more deeply/proposing a solution, that would be much appreciated. Thanks!
Does it work if you Inject ILoggerProvider
builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
also seeing this. we can't remove the default function logger providers (azure function checks it's expected runtime services) and when adding serilog we log duplicate traces :(
Any resolutions to this?
builder.Services.AddLogging(lb => { lb.ClearProviders().AddSerilog(Log.Logger); lb.AddSerilog(Log.Logger); });
Throws an error on start up in function app.
We use builder.Logging.ClearProviders().AddSerilog(); in an API but the above does not work and we are still getting duplicate logs.
We did add a TraceTelemetryConverter to set the CloudRoleName and did notice that 1 out of 2 of the duplicated logs reflects the name we set.
the marginal fix for this is to remove any AppInsights connection string / instrumentation key from the App/Function configuration section in Azure. with this connection string / instrumentation key removed, the app insights won't be set up for the app/function by default. you have to manually add/initialize it in the startup. then serilog logs will flow into AI as expected, without the duplicate Azure function runtime logger providers also logging to it.
unfortunately, doing this removes the operation_id type (End-to-end trasaction details view) grouping of the traces/requests/exceptions. 😢
the marginal fix for this is to remove any AppInsights connection string / instrumentation key from the App/Function configuration section in Azure. with this connection string / instrumentation key removed, the app insights won't be set up for the app/function by default. you have to manually add/initialize it in the startup.
unfortunately, doing this removes the operation_id type grouping of the traces/requests/exceptions. 😢
I was wondering about this and you just saved me a few hours of work. My conclusion is that with Azure Functions Net6 v4 in isolated mode that Serilog really isn't necessary. Application Insights will retain data for long enough time to debug and applications. This is good news and bad news. The bad news is that Serilog makes the logs more readable. And believe me, I have spent hours wiring up Serilog in several projects.
What we do is clear providers before adding Serilog.
- Add A.I.
- Clear all logging Providers
- Add Serilog
That removed all my duplicates when using logger & lets me use Serilog with the A.I. registered TelemetryClient.
What we do is clear providers before adding Serilog.
- Add A.I.
- Clear all logging Providers
- Add Serilog
That removed all my duplicates when using logger & lets me use Serilog with the A.I. registered TelemetryClient.
would you mind to share you code?
Thanks