有没有一种方法可以创建一个不包含文件名中的日期邮票的serilog非滚动文件接收器?
我是C#的新手 我们正在使用Serilog记录Ilogger日志记录。对于我的测试用例,我想将日志文件名传递给Serilog文件接收器,并且不要将Yyyymmdd插入文件名中。到目前为止,我还没有找到一种让Serilog不要将日期插入日志文件名中的方法。以下是缩写代码段以说明:
public static class Log
{
private static readonly ConcurrentDictionary<string, Lazy<ILogger>> fileLogs = new ConcurrentDictionary<string, Lazy<ILogger>>();
public static ILogger File(string path)
{
var filePath = string.IsNullOrEmpty(path)
? $"{Assembly.GetEntryAssembly().GetName().Name}_{UnixEpoch.MillisecondsNow}.log"
: path;
var fileInfo = new FileInfo(filePath);
return fileLogs.GetOrAdd(fileInfo.FullName, name => new Lazy<ILogger>(() =>
{
fileInfo.Directory.Create();
return LoggerFactory
.Create(builder => builder.AddFile(filePath))
.CreateLogger("File");
})).Value;
}
}
public void CreateFileLog()
{
var log = Log.File("./test.log");
log.LogInformation("Sample log record.");
}
输出文件名为:./test20220517.log
i i如何设置Serilog文件汇,以免将日期插入日志文件名中?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件接收器支持滚动间隔,这也是影响其写入文件的命名约定的原因。您可以设置Infinite 的滚动间隔,该间隔将无限期使用相同的文件,并且似乎不会更改文件名。
https://github.com/serilog/serilog-sinks-file#rolling-policies
请注意,文件汇将文件大小限制为1 GB,并将停止写入日志,直到下一个滚动周期到达下一个滚动周期防止磁盘使用问题,因此,如果您确实达到了限制,则无限日志可能会停止更新。
The File sink supports a rolling interval, which is also what impacts the naming convention of the file it writes to. You can set a rolling interval of infinite, which will use the same file indefinitely and doesn't appear to alter the file name.
https://github.com/serilog/serilog-sinks-file#rolling-policies
https://github.com/serilog/serilog-sinks-file/blob/dev/src/Serilog.Sinks.File/Sinks/File/RollingIntervalExtensions.cs#L26
Please be aware that the file sink limits the file size to 1 GB and will stop writing to the log until the next rolling period is reached to prevent disk usage issues, so if you do reach that limit, your infinite log may cease to update.