8

I'm using serilog in my ASP Core 2.2 application. Everything works great but I can't set flushToDiskInterval. It means that I want to flush logs to disk every minute for example but logs are flushed just as they're created. My Program.cs file:

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>();
}

So the question is how to set the interval?

==UPDATE==


I can't understand that but now everything works fine... I checked and there is really flush interval set.

Artyom
  • 654
  • 2
  • 7
  • 16
  • Is there any demo to reproduce your issue? I made a test with asp.net core 2.2, `Serilog.AspNetCore V2.0.0.0` and `Serilog.Sinks.File, Version=2.0.0.0`, it will write log interval. – Edward Jul 02 '19 at 06:18

3 Answers3

20

To set in the source code,

flushToDiskInterval: TimeSpan.FromSeconds(1)

or following setting in the appsettings.json,

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

flushToDiskInterval is in seconds.

Reference link.

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

just answering so that it may be useful to someone like me who is searching for answers!

Thanks, @Peter and @Carl Björknäs, answer is updated.

manu
  • 1,807
  • 4
  • 25
  • 32
  • 4
    I had problem with this setting, the flush was still slow. Changed to "flushToDiskInterval": "00:00:01" which made it behave as expected. – Carl Björknäs Mar 08 '21 at 10:31
  • I think @CarlBjörknäs is correct https://github.com/serilog/serilog-sinks-file/blob/771be4f77e5412dd01d27e217c25d02b0a954d44/src/Serilog.Sinks.File/FileLoggerConfigurationExtensions.cs#L71 please update the answer! – Peter Apr 06 '21 at 08:11
3

As suggested by Carl Björknäs, to set this parameter in the settings file use this notation

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

Because, internally when using Serilog Configuration, this method is used to parse the time string:

TimeSpan.Parse(s)

This is Serilog.Settings.Configuration snippet code:

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) },
            }; 
     ... 
}

This is TimeSpan.Parse Method Doc:

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

pampua84
  • 696
  • 10
  • 33
0

For .NET 6 add the following to your Program.cs file

var builder = WebApplication.CreateBuilder(args);

// other configuration stuff

builder.Host.UseSerilog((ctx, lc) => lc
    .WriteTo.Console()
    .WriteTo.File("logs/app.log",  
             rollingInterval: RollingInterval.Day, 
             flushToDiskInterval: TimeSpan.FromSeconds(5))
    );

// more configuration stuff

var app = builder.Build();
Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52