I have a .net framework application and a library that sets up Serilog. Here is the structure:
Edit:
program.cs
using Serilog;
using System;
using Library;
namespace SerilogTest
{
class Program
{
private static readonly ILogger Logger = Log.ForContext(typeof(Program));
static void Main(string[] args)
{
LoggerInstaller.Install();
Logger.Information("It works!");
Console.ReadLine();
}
}
}
LoggInstaller.cs
using Serilog;
namespace Library
{
static public class LoggerInstaller
{
static public Serilog.Core.Logger Install()
{
//SelfLog.Enable(Console.Error);
string logFileName = "MyLog.log";
var config = new LoggerConfiguration()
.ReadFrom.AppSettings()
.WriteTo.File(logFileName, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 50000)
.CreateLogger();
Log.Logger = config;
return config;
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="serilog:minimum-level" value="Verbose" />
<add key="serilog:using:Console" value="Serilog.Sinks.Console" />
<add key="serilog:write-to:Console" />
<add key="serilog:write-to:Console.outputTemplate" value="{Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" />
<add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
<add key="serilog:using:File" value="Serilog.Sinks.File" />
<add key="serilog:using:Thread" value="Serilog.Enrichers.Thread" />
<add key="serilog:enrich:WithThreadId" />
<add key="serilog:enrich:FromLogContext" />
<add key="serilog:using:Environment" value="Serilog.Enrichers.Environment" />
<add key="serilog:enrich:WithMachineName" />
<add key="serilog:using:Process" value="Serilog.Enrichers.Process" />
<add key="serilog:enrich:WithProcessId" />
<add key="serilog:using:Memory" value="Serilog.Enrichers.Memory" />
<add key="serilog:enrich:WithMemoryUsage" />
<add key="serilog:using:AssemblyName" value="Serilog.Enrichers.AssemblyName" />
<add key="serilog:enrich:WithAssemblyName" />
<add key="serilog:enrich:WithAssemblyVersion" />
<add key="serilog:enrich:with-property:Application" value="SerilogTest" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
When I run this code I get the following exception in the library
System.IO.FileNotFoundException: 'Could not load file or assembly 'Serilog.Sinks.Console' or one of its dependencies. The system cannot find the file specified.'
I have to include all the extra Serilog libraries in the SerilogTest app to get it to work. Why is this needed? Why is it not enough to have them in the library?