Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Specific folder structure #68

@jecar

Description

@jecar

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();
		 };
	 });
 });

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions