有没有一种方法可以创建一个不包含文件名中的日期邮票的serilog非滚动文件接收器?

发布于 2025-01-29 15:17:37 字数 1121 浏览 5 评论 0 原文

我是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文件汇,以免将日期插入日志文件名中?

I'm a newbie to C#...
We're using Serilog to record ILogger log records. For my test cases, I'd like to pass a log filename to Serilog File sink and have it not insert YYYYMMDD into the filename. So far, I have not found a way to get Serilog to not insert the date into the log filename. Below are abbreviated code snippets to demonstrate the point:

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)
            ? 
quot;{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.");
}

The output filename will be: ./test20220517.log

How do I setup Serilog File sink so it doesn't insert the date into the log filename?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2025-02-05 15:17:38

文件接收器支持滚动间隔,这也是影响其写入文件的命名约定的原因。您可以设置Infinite 的滚动间隔,该间隔将无限期使用相同的文件,并且似乎不会更改文件名。

要在c#代码中配置水槽,请在logger中调用writeto.file()
配置:

var log = new LoggerConfiguration()

.createlogger();这将把时间段附加到文件名中,创建一个文件集,例如:

log20180631.txt

log20180701.txt

log20180702.txt

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.

To configure the sink in C# code, call WriteTo.File() during logger
configuration:

var log = new LoggerConfiguration()
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger(); This will append the time period to the filename, creating a file set like:

log20180631.txt

log20180701.txt

log20180702.txt

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文