【问题标题】:Serilog set flush intervalSerilog 设置刷新间隔
【发布时间】:2019-07-01 11:07:45
【问题描述】:

我在我的 ASP Core 2.2 应用程序中使用 serilog。一切正常,但我无法设置flushToDiskInterval。例如,这意味着我想每分钟将日志刷新到磁盘,但日志在创建时会被刷新。 我的 Program.cs 文件:

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .WriteTo.File("Logs/log-{Date}.txt", buffered: true, flushToDiskInterval: TimeSpan.FromSeconds(60))
        .CreateLogger();

        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseSerilog() // Set serilog as the logging provider.
        .UseStartup<Startup>();
}

那么问题是如何设置间隔?

==UPDATE==


我不明白,但现在一切正常...我检查并确实设置了刷新间隔。

【问题讨论】:

  • 是否有任何演示可以重现您的问题?我用asp.net core 2.2,Serilog.AspNetCore V2.0.0.0Serilog.Sinks.File, Version=2.0.0.0做了一个测试,会写日志间隔。

标签: asp.net-core serilog


【解决方案1】:

要在源代码中设置,

flushToDiskInterval: TimeSpan.FromSeconds(1)

或在 appsettings.json 中进行以下设置,

"WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "restrictedToMinimumLevel": "Verbose",
          ...
          ...
          ...
          "flushToDiskInterval": 1
        }
      }
]

flushToDiskInterval 以秒为单位。

Reference link.

https://github.com/serilog/serilog-aspnetcore

只是回答,以便对像我这样正在寻找答案的人有用!

【讨论】:

【解决方案2】:

根据 Carl Björknäs 的建议,在设置文件中设置此参数使用此表示法

"flushToDiskInterval": "00:00:01"  //hh:mm:ss

因为,内部在使用 Serilog Configuration 的时候,会使用这个方法来解析时间字符串:

TimeSpan.Parse(s)

这是 Serilog.Settings.Configuration sn -p 代码:

class StringArgumentValue : IConfigurationArgumentValue
{
    readonly string _providedValue; 
    .....

    static readonly Dictionary<Type, Func<string, object>> ExtendedTypeConversions = new Dictionary<Type, Func<string, object>>
            {
                { typeof(Uri), s => new Uri(s) },
                { typeof(TimeSpan), s => TimeSpan.Parse(s) },
                { typeof(Type), s => Type.GetType(s, throwOnError:true) },
            }; 
     ... 
}

这是 TimeSpan.Parse 方法文档:

https://docs.microsoft.com/en-us/dotnet/api/system.timespan.parse?view=net-5.0

【讨论】:

    猜你喜欢
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多