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.
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.
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.
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.
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.
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With