Serilog配置
serilog-sinks-file
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="serilog:using:File" value="Serilog.Sinks.File" /> <add key="serilog:write-to:File.path" value="log\log.txt" /> <add key="serilog:write-to:File.rollOnFileSizeLimit" value="true" /> <add key="serilog:write-to:File.fileSizeLimitBytes" value="3072" /> <add key="serilog:write-to:File.shared" value="true" /> <add key="serilog:write-to:File.retainedFileCountLimit" value="null" /> </appSettings> </configuration>
void ApplyRetentionPolicy(string currentFilePath, DateTime now) { if (_retainedFileCountLimit == null && _retainedFileTimeLimit == null) return; }
Serilog配置不指定配置文件的路径
会直接从程序的默认配置文件中app.config加载appsettings结点下的配置
settings = ConfigurationManager.AppSettings.AllKeys .Select(k => new KeyValuePair<string, string>(k, ConfigurationManager.AppSettings[k]));
_settingPrefix = settingPrefix == null ? "serilog:" : $"{settingPrefix}:serilog:";
var pairs = settings .Where(k => k.Key.StartsWith(_settingPrefix)) .Select(k => new KeyValuePair<string, string>( k.Key.Substring(_settingPrefix.Length), Environment.ExpandEnvironmentVariables(k.Value))); loggerConfiguration.ReadFrom.KeyValuePairs(pairs);