serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
444 stars 129 forks source link

Serilog fails to load Sink's dll not referenced in the project but available in the application's folder #407

Closed yankun closed 8 months ago

yankun commented 8 months ago

Hi, I've encounter following problem:

I'm maintaining a solution which contains multiple projects being executables. The solution is using centralized package management. I'm publishing all of the projects into the same folder, to avoid duplication of all dlls involved. This worked great so far.

For now, all projects used the same sink, a file sink. Now one project is also using a console sink and that seems to be a problem.

If I start one of the projects only having a file sink, I will get following exception:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Serilog.Sinks.Console, Version=5.0.1.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10'. Das System kann die angegebene Datei nicht finden.
File name: 'Serilog.Sinks.Console, Version=5.0.1.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10'
   at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound)
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   at Serilog.Settings.Configuration.ConfigurationReader.LoadConfigurationAssemblies(IConfiguration section, AssemblyFinder assemblyFinder)
   at Serilog.Settings.Configuration.ConfigurationReader..ctor(IConfiguration configSection, AssemblyFinder assemblyFinder, ConfigurationReaderOptions readerOptions, IConfiguration configuration)
   at Serilog.ConfigurationLoggerConfigurationExtensions.GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, DependencyContext dependencyContext)
   at Serilog.ConfigurationLoggerConfigurationExtensions.Configuration(LoggerSettingsConfiguration settingConfiguration, IConfiguration configuration, ConfigurationReaderOptions readerOptions)

The failing project has no reference to the console sink (and no such sink configured), but as everything is copied into the same directory, the dll is available. If I delete the dll, or adding a reference to it for the related project, everything works. So this only happens, if the project doesn't reference the sink in any way, but the dll is yet available in the applications folder.

What could cause this? Also I think it is strange, that Serilog tries to load assemblies not being required by the provided logger configuration.

bartelink commented 8 months ago

Better to log this on stackoverflow.com as anyone watching this repo is going to be watching the serilog tag there too, and there's much better reach for people looking to diagnose similar issues in the future when it gets worked out there - be sure tho include relevant code and/or config snippets to convey how you've arranged things.

Feel free to post a link here when done.

bartelink commented 8 months ago

https://stackoverflow.com/questions/77759166/serilog-fails-to-load-sinks-dll-not-referenced-in-the-project-but-available-in