Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specific folder structure #68

Open
jecar opened this issue Aug 9, 2024 · 1 comment
Open

Specific folder structure #68

jecar opened this issue Aug 9, 2024 · 1 comment
Labels

Comments

@jecar
Copy link

jecar commented Aug 9, 2024

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();
		 };
	 });
 });
@VitaliyMF
Copy link
Contributor

@jecar sorry for the late response.

Is the configuration below the correct way to achieve this?

yes, as this approach is mentioned in README

Will the file/folder rotation occur at midnight as expected when using this in an ASP.NET application?

I guess you're referring to "rolling file" behaviour. It works as long as your "FormatLogFileName" returns the same value. When it returns a new value, this automatically creates a new log file, and if this new path is for another (new and empty) folder everything retated to rolling starts from the beginning.

Will the file rotation upon exceeding the size limit follow the formatting rule and handle the day change correctly, examples :

It's easy to predict rolling files behaviour by realizing that all this logic works only when "FormatLogFileName" returns the same value, and in this case the behaviour is the same as if LogFileName is hardcoded (like 'test.log'). New day leads to a new LogFileName, and everything that was before that doesn't matter for rolling file behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants