c# – 如何在Serilog中使用ReadFrom.AppSettings

我刚刚发现了Serilog并且我喜欢它.但是,我很难让它从app.config读取.

代码配置:

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;

注释掉的部分是我想从配置文件中读取的配置值.

配置文件包含:

<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>

我正在使用以下Serilog软件包.

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

我还将此添加为我的控制台应用程序的第一行.我没有在控制台上看到任何错误.

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

我错过了什么?

最佳答案 看起来您的配置可能有拼写错误:

serilog:writeto:MSSqlServer.tableName

缺少破折号:

serilog:write-to:MSSqlServer.tableName
点赞