-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate Logs Using Serilog Logger in Azure Function #157
Comments
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.
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?
Throws an error on start up in function app. 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. 😢 |
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.
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 |
I ** you can remove the default provider and add Serilog like belowStartup.cs
Logging sample
|
That would be strange, as this is controlling the sampling settings for application insights. It is not removing or disabling the app insights logger. (see docs) |
@fgheysels Try the second approach mentioned in my comments. It works for me. Currently, I`m using it. |
An instance of ApplicationInsightsLoggerProvider is registered and used by the In-Process Azure Function Host using the LoggerProvider pattern to log to App Insights. This works fine until you want to log custom things to App Insights (using Serilog, etc) because by registering your custom App Insights provider, your loggers will now write to App Insights twice; once through the Azure Host registered logger provider and once through your custom logger provider. To fix this, you could completely remove the Azure Host registered logger provider, but this removes all the nice, default metric/request/correlation capturing capabilities of Az Funcs + App Insights. The working solution I have found is to register a custom LoggerFactory instance that contains all the registered logger providers except the App Insights logger provider registered by the Azure Function Host that also contains any custom provider that I want to use to log to App Insights. Then, you can inject this LoggerFactory instance into your Function App class and create an ILogger instance that logs to App Insights only through your custom provider and still logs to all other providers and does not require you to remove the default App Insights logger provider completely. This way, you get the best of both worlds. In this model, you would not register your custom logger provider with the ILoggerProvider interface since this would cause all ILogger instances directly resolved from the container to use both App Insights providers, leading to duplicate logs. Startup Configure method:
Function code:
|
Duplicate logs issue occurs in Azure Functions when the following environment variables are used. These variables enable ApplicationInsights in the runtime host used by the Azure Functions service. The Azure Functions runtime builds upon the Azure WebJobs SDK to provide a hosting platform for functions. So you have to somehow bypass using these environment variables, if you want to use Serilog.Sinks.ApplicationInsights package with azure functions.
If you check SDK version property in AppInsights logs.
For isolated worker processSolution 1If you set connection string or instrumentation key in serilog config file, you won't get duplicate logs issue. Example: Solution 2 (Serilog configuraion is in appsettings.json)Reference: https://github.com/serilog/serilog-settings-configuration#serilogsettingsconfiguration--
Solution 3Create a cutom environment variable like
For In-processSolution 1https://github.com/serilog/serilog-settings-configuration#azure-functions-v2-v3 References:
|
I've had a similar issue to what's been discussed above, but with Blazor application using the application insights sink. Not sure where to post this, so posting here in the hope that someone else doesn't waste a couple hours. Reproduction steps:
and added Serilog from configuration.
This is a non-code solution to my specific scenario, but hopefully this is an easy thing to rule out if you run into the same issues I have when trying to use Serilog in Azure! |
This is a helpful solution, but it seems like if we remove the default host logger it prevents other telemetry types from being sent. Such as Request, Dependency, and Custom events. Do you have a workaround for that also? |
I seeing fixes where the recommendation is to remove the default Application Insights Logger but there are issues that can come with this. The biggest one that I can think of is that you no longer have the ability to send Request and Dependency events to AI. Is there really no way to tell the default Application Insights logger to stop sending trace events and leave that to serilog? |
Interesting solution but this does not use Serilog for trace event logs |
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)
The text was updated successfully, but these errors were encountered: