0

I have an Enterprise Logging programmatic configuration that looks like this:

        builder.ConfigureLogging()
               .LogToCategoryNamed("General")
               .WithOptions.SetAsDefaultCategory()
               .SendTo.RollingFile("Rolling Flat File Trace Listener")
               .CleanUpArchivedFilesWhenMoreThan(7)
               .WhenRollFileExists(RollFileExistsBehavior.Increment)
               .WithTraceOptions(TraceOptions.Timestamp)
               .RollEvery(RollInterval.Day)
               .UseTimeStampPattern("yyyy-mm-dd")
               .ToFile(logPath)
               .FormatWith(
                   new FormatterBuilder()
                       .TextFormatterNamed("Text Formatter")
                       .UsingTemplate("{timestamp}:{title}:{message}"))
               .WithFooter("").WithHeader("");

And I'm not seeing the rolling behavior, but am seeing occasional log file corruption. Anyone have any suggestion/ideas?

babbitt
  • 871
  • 11
  • 22

1 Answers1

0

A few thoughts:

For the timestamp pattern "yyyy-mm-dd", mm is minutes. You probably want to use "yyyy-MM-dd".

RollInterval.Day means that the file will only roll 24 hours after the log file was created. For example if the first LogEntry is written at Saturday at 4pm then a new file will be created. If the next entry is written Sunday at 5pm then the file will roll and a new file will be created but the new file will not be eligible to roll until Monday at 5pm (since the current log file was created at 5pm). If you want to roll when the first LogEntry is written after Midnight then you can use RollInterval.Midnight.

Your code seems to work fine for me.

You also might want to create an errors special source to log any errors that might occur in the block:

var builder = new ConfigurationSourceBuilder();
builder.ConfigureLogging()
                .LogToCategoryNamed("General")
                .WithOptions.SetAsDefaultCategory()
                .SendTo.RollingFile("Rolling Flat File Trace Listener")
                .CleanUpArchivedFilesWhenMoreThan(7)
                .WhenRollFileExists(RollFileExistsBehavior.Increment)
                .WithTraceOptions(TraceOptions.Timestamp)
                .RollEvery(RollInterval.Day)
                .UseTimeStampPattern("yyyy-MM-dd")
                .ToFile(logPath)
                .FormatWith(
                    new FormatterBuilder()
                        .TextFormatterNamed("Text Formatter")
                        .UsingTemplate("{timestamp}:{title}:{message}"))
                .WithFooter("").WithHeader("")
                .SpecialSources
                    .LoggingErrorsAndWarningsCategory
                    .SendTo.FlatFile("Flat File Trace Listener")
                    .ToFile("errors.log");
Randy Levy
  • 22,566
  • 4
  • 68
  • 94