Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ReadFrom.AppSettings in Serilog

Tags:

c#

.net

serilog

I just discovered Serilog and I love it. However, I'm struggling to get it to read from app.config.

Code Configuration:

ILogger logger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    //.MinimumLevel.Verbose()
    .Enrich.WithProcessId()
    .Enrich.WithThreadId()
    .Enrich.WithMachineName()
    .Destructure.UsingAttributes()
    //.WriteTo.MSSqlServer(@"Server=EVDVWADBV1;Database=AppLog;Trusted_Connection=True;", "Logs")
    .CreateLogger();
Log.Logger = logger;

The commented out sections are the configuration values I want to read from a config file.

The config file contains:

<appSettings>
    <add key="serilog:minimum-level" value="Verbose"/>
    <add key="serilog:using" value="Serilog.Sinks.MSSqlServer"/>
    <add key="serilog:write-to:MSSqlServer.connectionString" value="Server=EVDVWADBV1;Database=AppLog;Trusted_Connection=True;"/>
    <add key="serilog:writeto:MSSqlServer.tableName" value="Logs"/>
    <add key="serilog:write-to:RollingFile.pathFormat" value="C:\Logs\myapp-{Date}.txt" />
    <add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />
</appSettings>

I'm using the following Serilog packages.

<package id="Serilog" version="1.5.14" targetFramework="net452" />
<package id="Serilog.Sinks.MSSqlServer" version="3.0.41" targetFramework="net452" />

I also added this as the first line of my console app. I'm not seeing any errors on the console.

Serilog.Debugging.SelfLog.Out = Console.Out;

What am I missing?

like image 554
Candi Suriano Avatar asked Oct 19 '22 16:10

Candi Suriano


1 Answers

It looks like you may have a typo in the configuration:

serilog:writeto:MSSqlServer.tableName

Missing the dash:

serilog:write-to:MSSqlServer.tableName
like image 108
Nicholas Blumhardt Avatar answered Oct 21 '22 06:10

Nicholas Blumhardt