lundi 20 juillet 2020

How to use NuGet packages for Serilog Sinks that reside in another project within same solution?

I am working on a multi project solution in which we have implemented a shared project where our logging-abstraction resides. All of our other projects in the solution acquires a logger from this project when needed. In each of the other projects, I have placed a serilog.json file with the configuration for Serilog that I want for the given project, thinking that this would allow me to only install the NuGet-packages for Serilog and the Sinks in the shared project. However, I can only get Serilog to run when I install the NuGet-package for the needed sink in the given project. To utilize a serilog.json-file from a given project, I have the following code in the shared project:

var localPathToExecutingAssembly = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
var directoryName = Path.GetDirectoryName(localPathToExecutingAssembly);
var completeFilePath = Path.Combine(directoryName, "serilog.json");
var configuration = new ConfigurationBuilder().AddJsonFile(completeFilePath).Build();

A given serilog.json-file could look like this:

{
  "Serilog": {
    "Using":  [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Debug",
...
}

This produces the following exception:

System.IO.FileNotFoundException: Could not load file or assembly 'Serilog.Sinks.Console' or one of its dependencies. The system cannot find the file specified.

Because Serilog.Sinks.Console have only been installed in the shared project, in which I also execute the following with the aforementioned Configuration:

var logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration, DependencyContext.Default)
                         .CreateLogger();

logger.Fatal("Hello, world!") 

In this example, I bypass the architecture for acquiring loggers and abstracting away the chosen framework for this; that is on purpose! :-)

What do I need to pass for DependencyContext to make this work without installing the NuGet-packages all over the solution?





Aucun commentaire:

Enregistrer un commentaire