Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog RollingFile

Tags:

c#

.net

serilog

I am trying to use WriteTo.RollingFile with Serilog as the following to write one file per day:

var log = new LoggerConfiguration().WriteTo.RollingFile(                     @"F:\logs\log-{Date}.txt",                     LogEventLevel.Debug).CreateLogger();             log.Information("this is a log test"); 

But I am getting a new log file for each log entry during the same day!

How does one configure Serilog to write to a new file every day as to have a have a single log file per day?


And is there any archiving process to delete files older than 7 days?

like image 556
TechNerd Avatar asked Aug 20 '15 01:08

TechNerd


People also ask

Is Serilog deprecated?

This package has been deprecated as it is legacy and is no longer maintained.

What is Serilog .NET core?

Serilog is a . NET library that provides diagnostic logging to files, the console, and almost everywhere you would like. Serilog can be used in classic . NET Framework applications and for applications running on the latest and greatest .

What is Serilog sink?

Serilog is a structured logging library for Microsoft . NET and has become the preferred logging library for . NET at Checkout.com.. It supports a variety of logging destinations, referred to as Sinks, from standard console and files based sinks to logging services such as Datadog.

Where does Serilog write to by default?

By default, serilog will only log to the console.


2 Answers

Try below:

 var log = new LoggerConfiguration()           .MinimumLevel.Debug()           .WriteTo.File(@"f:\log\log.txt", rollingInterval: RollingInterval.Day)            .CreateLogger(); 

The log file name will be automatically log-20150819.txt etc. You do not need to specify the date.Old files will be cleaned up as per retainedFileCountLimit - default is 31.

like image 126
Infinity Challenger Avatar answered Sep 20 '22 15:09

Infinity Challenger


WriteTo.RollingFile Deprecated

Since the posted answer, the method RollingFile is no longer viable and replaced with File and options to roll the logs.

Now one must use the standard Serilog.Sinks.File NuGet package which supports rolling:

.WriteTo.File(path: @"e:\logs\skilliam.log",                rollingInterval: RollingInterval.Day,               rollOnFileSizeLimit: true,                fileSizeLimitBytes: 123456); 
like image 20
Mohammed Noureldin Avatar answered Sep 20 '22 15:09

Mohammed Noureldin