-
Notifications
You must be signed in to change notification settings - Fork 64
Open
Labels
Description
First of all, thank you for this great logging implementation — simple yet powerful.
I store my log files in a specific folder structure: \ProjectName\[Year]\[Month]\[Day].txt
I have three questions:
-
Is the configuration below the correct way to achieve this?
-
Will the file/folder rotation occur at midnight as expected when using this in an ASP.NET application?
-
Will the file rotation upon exceeding the size limit follow the formatting rule and handle the day change correctly, examples :
\ProjectName\2024\08\09.txt
\ProjectName\2024\08\091.txt
\ProjectName\2024\08\092.txt
\ProjectName\2024\08\10.txt
.AddLogging(loggingBuilder =>
{
IConfiguration configuration = hostContext.Configuration;
var loggingSection = configuration.GetSection("Logging");
loggingBuilder.AddConfiguration(loggingSection);
loggingBuilder.AddFile(loggingSection.GetSection("File"), fileLoggerOpts =>
{
fileLoggerOpts.FormatLogFileName = fName =>
{
var nameFormat = string.Format("{0:dd}.txt", DateTime.UtcNow);
var path = Path.Combine(fName, DateTime.Now.Year.ToString(), DateTime.Now.ToString("MM"));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return Path.Combine(path, nameFormat);
};
fileLoggerOpts.FileSizeLimitBytes = 1000000; //10Mo
fileLoggerOpts.MaxRollingFiles = 10;
fileLoggerOpts.FormatLogEntry = (msg) =>
{
var sb = new System.Text.StringBuilder();
sb.Append(DateTime.Now.ToString("HH:mm:ss.fff"));
sb.Append(" ");
sb.Append(msg.LogLevel.ToString());
sb.Append(" ");
sb.Append(msg.Message);
sb.Append(" ");
sb.Append(msg.LogName);
sb.Append(" ");
sb.Append(msg.EventId.Id);
sb.Append(" ");
sb.Append(msg.Exception?.ToString());
return sb.ToString();
};
});
});