Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog set flush interval

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.

like image 843
Artyom Avatar asked Jul 01 '19 11:07

Artyom


People also ask

What is Minimumlevel in Serilog?

Serilog defines several levels of log events. From low to high, these are Verbose , Debug , Information , Warning , Error and Fatal . You can set the minimum level you want to log, meaning that events for that level or higher will be logged.

What is Serilog Aspnetcore?

Serilog is a third-party, open-source library that integrates nicely with ASP.NET Core and allows developers to easily log-structured event data to the console, to files, and various kinds of log targets.

What is Serilog used for?

NET, Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is portable between recent . NET platforms. Unlike other logging libraries, Serilog is built with powerful structured event data in mind.

What is Serilog sink?

Serilog provides sinks for writing log events to storage in various formats. Many of the sinks listed below are developed and supported by the wider Serilog community; please direct questions and issues to the relevant repository. More sinks can be found by searching within the serilog tag on NuGet.


2 Answers

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": 1
        }
      }
]

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!

like image 130
manu Avatar answered Nov 02 '22 16:11

manu


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://docs.microsoft.com/en-us/dotnet/api/system.timespan.parse?view=net-5.0

like image 30
pampua84 Avatar answered Nov 02 '22 16:11

pampua84