dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.65k stars 25.29k forks source link

Clarify Logging AddEventLog Overloads Usage #32746

Open xxAtrain223 opened 5 months ago

xxAtrain223 commented 5 months ago

Description

Please clarify that when using AddEventLog, the EventLogSettings overload should only be used if builder.Logging.ClearProviders(); was called, otherwise the new EventLogSettings doesn't get used. And that if you don't ClearProviders, then the Action<EventLogSettings> overload should be used.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-8.0#welog

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/logging/index.md

Document ID

726e3bf1-f367-d733-8933-bccc04da0e16

Article author

@tdykstra


Associated WorkItem - 261933

tdykstra commented 5 months ago

Response from GitHub Copilot:

The statement is partially correct. When you use AddEventLog with the EventLogSettings overload, it creates a new instance of EventLogLoggerProvider with the provided settings. If there's already an EventLogLoggerProvider instance registered (which would be the case if you didn't call builder.Logging.ClearProviders()), the new settings won't replace the existing ones.

However, the second part of the statement is not entirely accurate. The Action<EventLogSettings> overload of AddEventLog doesn't replace the settings of an existing EventLogLoggerProvider instance either. Instead, it modifies the settings of the EventLogLoggerProvider instance that's being added.

So, if you want to ensure that your EventLogSettings are used, you should call builder.Logging.ClearProviders() before calling AddEventLog. If you don't clear the providers and there's already an EventLogLoggerProvider registered, neither overload of AddEventLog will replace the existing settings.

xxAtrain223 commented 4 months ago

Please note that there has been confusion from this since 2021: https://github.com/dotnet/runtime/issues/51899