简体   繁体   English

读取serilog的appconfig文件

[英]reading appconfig file for serilog

I have Logger.cs class where I am initializing serilog settings as follows: 我有Logger.cs类,我正在初始化serilog设置,如下所示:

_logger = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .MinimumLevel.Debug()
                .WriteTo.File(_filepath, restrictedToMinimumLevel: LogEventLevel.Debug, shared: true, rollOnFileSizeLimit: true)
                .CreateLogger();

I want to read the size of the file from app.config file. 我想从app.config文件中读取文件的大小。 I have the app.config file as below 我有app.config文件如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="serilog:using:File" value="Serilog.Sinks.File" />
    <add key="serilog:write-to:File.fileSizeLimitBytes" value="2000" />
  </appSettings>
</configuration>

But looks like appsettings is not read at all. 但看起来像appsettings根本没有阅读。 because I can see more than 2kb file getting generated. 因为我可以看到生成的文件超过2kb。 What I have missed here? 我错过了什么?

How my logger class will read from app.config file, have I missed any settings in assemblyinfo class? 我的记录器类将如何从app.config文件中读取,我是否错过了assemblyinfo类中的任何设置?

The Serilog configuration via App.config does not "merge" with the configuration that you have defined via C# code... These are additive. 通过App.config的Serilog配置不会与您通过C#代码定义的配置“合并”......这些是附加的。 Meaning that, in your example, you are configuring two independent sinks, that are both writing to a file. 这意味着,在您的示例中,您正在配置两个独立的接收器,它们都写入文件。

However, because you did not specify the file path for the sink in the App.config, it's ignoring that sink and not configuring it, and only the second sink is being configured (in the C# code). 但是,因为您没有在App.config中指定接收器的文件路径,所以它忽略了接收器并且没有配置它,并且只配置了第二个接收器(在C#代码中)。

If you want to use the App.config configuration, then your XML should include a file path, in addition to the fileSizeLimitBytes : 如果要使用App.config配置,那么除了fileSizeLimitBytes之外,XML还应包含文件路径:

<configuration>
  <appSettings>
    <add key="serilog:minimum-level" value="Debug"/>
    <add key="serilog:using:File" value="Serilog.Sinks.File" />
    <add key="serilog:write-to:File.path" value="log.txt" />
    <add key="serilog:write-to:File.rollOnFileSizeLimit" value="true" />
    <add key="serilog:write-to:File.fileSizeLimitBytes" value="2000" />
  </appSettings>
</configuration>

And your C# code would just read the settings from the App.config, without the "extra" sink. 而你的C#代码只是从App.config中读取设置,没有“额外”接收器。

_logger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    .CreateLogger();

ps: Notice that I also configured the MinimumLevel via App.config. ps:请注意,我还通过App.config配置了MinimumLevel That's not a requirement, but usually makes sense if are already configuring Serilog sinks via App.config. 这不是一个要求,但如果已经通过App.config配置Serilog接收器通常是有意义的。

You can mix and match XML and code-based configuration, but each sink must be configured either using XML or in code-sinks added in code can't be modified via app settings. 您可以混合搭配XML和基于代码的配置,但每个接收器必须使用XML配置,或者在代码中添加代码接收器,不能通过应用程序设置进行修改。

So if you need to use App.config file you will need to move all configuration to it as below 因此,如果您需要使用App.config文件,则需要将所有配置移动到它,如下所示

<appSettings>
  <add key="serilog:minimum-level" value="Debug"/>
  <add key="serilog:using:File" value="Serilog.Sinks.File" />
  <add key="serilog:write-to:File.path" value="logs\log.txt" />
  <add key="serilog:write-to:File.shared" value="true" />
  <add key="serilog:write-to:File.rollOnFileSizeLimit" value="true" />
  <add key="serilog:write-to:File.fileSizeLimitBytes" value="2000" />
</appSettings>

And creating logger instance using below code 并使用下面的代码创建记录器实例

_logger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    .CreateLogger();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM