Single log file - serilog

I would like to have a single log file that will be rolled on the size limit, previous file will be removed so there is only one log file at a time. Exaxmple:
logs.txt raches 10MB --> delete logs.txt start writing to logs_001.txt
My current code is:
Log.Logger = new LoggerConfiguration()
.WriteTo.File(
LogFile,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 1,
fileSizeLimitBytes: 10485760) //10MB
.CreateLogger();
The code is from a Xamarin Forms project and it's executed every time the application is initialized.
The issue with that code is that on each application initialization a new log file is created, the previous one is deleted but the file size limit is not respected. So if the log file size is
lower than 10MB it will still roll to a new file at each start of the application.

The solution was to simply remove rollOnFileSizeLimit: true

Related

Serilog - keep only 7 latest files

I use serilog and have the date as a part of my filename. This is an easy way to get to the file. Currently I am checking nightly events and I and just pick the last file in the morning.
Now, I only want to keep 7 days. This is was retainedFileCountLimit is for.
However that does not work as I want it too, as it might check for that specific filename.
How can I do this? (I had my own log system which deleted files older than a week)
Where are all serilog properties described? I am missing an overview of those.
//Add Serilog
string logFileName = HostingEnvironment.MapPath(#"~/new_" + DateTime.Now.ToString("yyyyMMdd") + ".log");
Log.Logger = new LoggerConfiguration()
.WriteTo.File(
path: logFileName,
retainedFileCountLimit: 7,
shared: true,
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
fileSizeLimitBytes: 123456,
flushToDiskInterval: TimeSpan.FromSeconds(5))
.CreateLogger();
Log.Information("Starting Serilog #1");
The File sink automatically includes the date in the file name - do not include DateTime.Now in the file name and let Serilog take care of that and you should get the retention that you expect.
var log = new LoggerConfiguration()
.WriteTo.File
(
"new_.txt", // <<<<<<<<<<<<<<<<<<<<<<
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7,
// ...
)
.CreateLogger();
This will append the time period to the filename, creating a file set like:
new_20180631.txt
new_20180701.txt
new_20180702.txt
The documentation of the File sink is the repository on GitHub.

Creating log backup file with .bak extension using serilog

I am using Serilog framework for logging in my application. The file size limit i have given is 2MB. So when the file reaches 2MB, new file is created with like app_001.log, existing app.log is a back up file.
But what i want to do is when the file reaches 2MB, it should rename app.log to app.log.bak and write the new logs to newly created app.log file.
_logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(_filepath, restrictedToMinimumLevel: LogEventLevel.Debug, shared: true, rollOnFileSizeLimit: true, fileSizeLimitBytes: 2000000)
.CreateLogger();
You can create a class that derives from FileLifecycleHooks and override OnFileOpened and add some logic to check for the existence of app_*.log files and rename them to app_*.bak.
https://github.com/serilog/serilog-sinks-file#extensibility

Errors on System.IO.FileStream CSV file creation and System.IO.File.Delete

In an old VB.NET code (VS 2008) I use this code:
If System.IO.File.Exists(sFilePath) Then
System.IO.File.Delete(sFilePath)
End If
Dim Fs As New System.IO.FileStream(sFilePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write)
Fs.Close()
If the file exists then on this line:
System.IO.File.Delete(sFilePath)
I get this error:
The process cannot access the file because it is being used by another process.
When I manually remove the file, then on this line:
Dim Fs As New System.IO.FileStream(sFilePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write)
I get the error:
The file already exists
Although it doesn't.
The code always (for 8 years now) worked perfectly. Nothing in this part of the code has been changed.
So what is going on here?

What to do about huge stacktrace.log file in grails

The project I'm working on has a stacktrace.log file that is over 160GB in space. This is killing my hardrive space. What can I do to avoid this.
You should use rolling file appender so that the log file does not grow that huge size.
Use configuration like:
rollingFile name:'stacktrace', file:'stacktrace.log',
maxFileSize:'100MB', maxBackupIndex:5
Here every log file will be maximum 100 MB. You can control how many previous file will be existed by 'maxBackupIndex'.
You can empty the existing huge file by(in linux)
cat /dev/null > /path/to/file/stacktrace.log

NLog File splitting

I'm using NLog to log to file. Is there a way to configure it to create a new log file when the current one reaches a certain threshold (eg ~50mb)? Can it be done from the configuration file or code?
Yes:
fileName="${basedir}/logs/logfile.txt"
archiveFileName="${basedir}/archives/log.{#####}.txt"
archiveAboveSize="5242880"
archiveNumbering="Sequence"
concurrentWrites="true" <!-- http://nlog-project.org/doc/2.0/sl2/html/P_NLog_Targets_FileTarget_ArchiveAboveSize.htm -->

Resources