serilog-sinks-applicationinsights icon indicating copy to clipboard operation
serilog-sinks-applicationinsights copied to clipboard

Duplicate Logs Using Serilog Logger in Azure Function

Open eldursi opened this issue 4 years ago • 22 comments

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)

eldursi avatar Mar 18 '21 10:03 eldursi

Any update on this?

davidferro91 avatar Oct 14 '21 14:10 davidferro91

any other solution? I have this same problem in the database

GianLucaFinelli avatar Oct 29 '21 12:10 GianLucaFinelli

I'm also experiencing this.

gradyal avatar Nov 26 '21 21:11 gradyal

Same issue here. Any solution or workaround available?

catsburg avatar Feb 14 '22 11:02 catsburg

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.

CrisZis01 avatar Apr 12 '22 18:04 CrisZis01

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!

nblumhardt avatar May 01 '22 23:05 nblumhardt

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

zyofeng avatar May 12 '22 02:05 zyofeng

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 :(

ckarcz avatar Jun 16 '22 15:06 ckarcz

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.

jefferyblessing avatar Aug 08 '22 21:08 jefferyblessing

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. 😢

ckarcz avatar Sep 20 '22 15:09 ckarcz

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.

jassent avatar Sep 20 '22 17:09 jassent

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.

wkoeter avatar Nov 21 '22 14:11 wkoeter

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

middiu avatar Dec 01 '22 22:12 middiu