所以我有一个简单的方法为我制作记录器实例。
它如下
protected ILogger GetIndividualLogger(ILogger parentLogger, LogEventLevel minimumLevel, string RootDir, string Type, string Name, bool shouldBuffer, bool shouldBeShared, TimeSpan flushToDiskInterval)
{
var LogAllFile = $"{RootDir}{Path.DirectorySeparatorChar}{Name}{Path.DirectorySeparatorChar}RAL-{Type}-{Name}-.log";
var LogWarningFile = $"{RootDir}{Path.DirectorySeparatorChar}{Name}{Path.DirectorySeparatorChar}Warnings{Path.DirectorySeparatorChar}RAL-{Type}-{Name}-.log";
ILogger logger = new LoggerConfiguration()
.MinimumLevel.Is(minimumLevel)
.Enrich.WithDemystifiedStackTraces()
.WriteTo.Async(a => a.Logger(parentLogger))
.WriteTo.Async(a => a.File(LogAllFile, outputTemplate: SerilogHereHelper.TemplateForHere, rollingInterval: RollingInterval.Day, buffered: shouldBuffer, shared: shouldBeShared))
.WriteTo.Async(a => a.File(LogWarningFile, restrictedToMinimumLevel: LogEventLevel.Warning, outputTemplate: SerilogHereHelper.TemplateForHere, rollingInterval: RollingInterval.Day, buffered: shouldBuffer, shared: shouldBeShared, flushToDiskInterval: flushToDiskInterval))
.CreateLogger();
return logger;
}
以下是对GetIndividualLogger的示例调用
GetIndividualLogger(parentLogger: mainLogger, minimumLevel: LogEventLevel.Information, RootDir: _stackLightLogPath, Type: "Stack Light", Name: "Stack Light 1", shouldBuffer: true, shouldBeShared: false, flushToDiskInterval: TimeSpan.FromSecond(2))
警告级别接收器将写入,但是另一个接收器(通常设置为调试或信息)将创建一个空文件但从不向其添加任何条目。
我希望这两个文件都具有所有警告级别条目或更高级别。我希望“LogAllFile”的所有条目都达到或超过最低级别,但它是空白的。
我的记录器配置有问题吗?或者我的问题在别的地方?
所以这个问题的答案很愚蠢。警报日志每2秒刷新一次到磁盘,但是主日志文件不是那么我需要做的就是为flushToDiskInterval传递一个值
.WriteTo.Async(a => a.File(LogAllFile, outputTemplate: SerilogHereHelper.TemplateForHere, rollingInterval: RollingInterval.Day, buffered: shouldBuffer, shared: shouldBeShared, flushToDiskInterval: flushToDiskInterval))