serilog / serilog-settings-configuration

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

System.IO.FileNotFoundException when try to dynamically load dll #283

Open sasha-khadasevich opened 3 years ago

sasha-khadasevich commented 3 years ago

Unable to load dynamically .dll to .asp net core app.

Steps to reproduce: -create default asp net core web api app and add Serilog.AspNetCore and Serilog.Settings.Configuration packages -update CreateHostBuilder

.UseSerilog((context, services, configuration) =>
{
   configuration.ReadFrom.Configuration(context.Configuration, ConfigurationAssemblySource.AlwaysScanDllFiles);
})

-copy paste Serilog.Sinks.Grafana.Loki.dll (for example) to bin\Debug\netcoreapp3.1 -add Grafana.Loki configuration to appSettings.json

{
   "Serilog":{
      "MinimumLevel":"Debug",
      "WriteTo":[
         {
            "Name":"GrafanaLoki",
            "Args":{
               "uri":"http://localhost:3100"
            }
         }
      ]
   }
}

In the result I have exception: System.IO.FileNotFoundException: 'Could not load file or assembly 'Serilog.Sinks.Grafana.Loki, Version=7.0.2.0, Culture=neutral, PublicKeyToken=e33b67d3bb5581e4'. The system cannot find the file specified.'

nomailme commented 2 years ago

Yep same thing happens with my minimal repro. If anyone interested I can upload it(minimal repro) to github

EugeneAzhogin commented 4 months ago

I'm using a workaround like this

var sinks = builder.Configuration.GetSection("Serilog:Using").Get<string[]>();
var sinkAssemblies = sinks?.Select(sink => Assembly.LoadFrom($"{sink}.dll")).ToArray();
var configurationReaderOptions = sinkAssemblies != null && sinkAssemblies.Length > 0
    ? new ConfigurationReaderOptions(sinkAssemblies) { SectionName = "Serilog" }
    : new ConfigurationReaderOptions() { SectionName = "Serilog" };

builder.Host
    .UseSerilog((context, services, configuration) => configuration
        .ReadFrom.Configuration(context.Configuration, configurationReaderOptions)
        .ReadFrom.Services(services), preserveStaticLogger: false, writeToProviders: false);