0

I'd like System.Diagnostics.EventSchemaTraceListener to generate an unlimited set of sequential files, so I initialised one with TraceLogRetentionOption.UnlimitedSequentialFiles. However, the trace output was written to one single file rather than multiple sequential files.

Is there any official or unofficial documentation or code sample on using this specific log retention option? And failing that, is my code below incorrect in any way?


To reduce magic configuration woo, I'll do everything in code. Here's how I think it's supposed to be initialised:

TraceSource ts = new TraceSource("My trace source", SourceLevels.All);
SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose");
ts.Switch = sourceSwitch;
ts.Listeners.Add(new System.Diagnostics.EventSchemaTraceListener(
  "event.log", // "file name"
  "eventschema", // "name"
  1024, // buffer in bytes - I set this to be <= maximum file size
  TraceLogRetentionOption.UnlimitedSequentialFiles,
  1024); // max. file size in bytes - very small to illustrate the concept

And then I log to it a few times:

for (var i = 0; i < aFew; i++) {
  ts.TraceData(
    TraceEventType.Information, // trace level
    100, // event ID: arbitrary integer
    "some data to output");
}

I observe a single event.log file is created and contains all the expected information, but the TraceLogRetentionOption is not respected as the single file's size exceeds the maximum 1kB specified.

What am I doing wrong?

Jeremy
  • 2,642
  • 18
  • 36

2 Answers2

1

From what it looks like, the file size limit you impose is only a threshold if the size of the message being output exceeds the size that you specify. Citing the MSDN's MaximumFileSizeProperty page:

The property value is set by the maximumFileSize parameter in the constructor or the maximumFileSize attribute in the configuration file. For performance reasons, you should set the maximum file size to a multiple of 1024 bytes. The MaximumFileSize property value is not an absolute; it is a threshold that can be exceeded up to the size of the last message.

MSalmo
  • 352
  • 1
  • 14
  • Thanks — I saw that in the docs, and interpreted it to mean that when starting a new trace event/data/whatever, the listener should create a new file if the existing one is over the size limit. Regardless, I tried logging more than 1024 bytes in one hit, and it did not affect the outcome. – Jeremy Mar 11 '15 at 05:37
  • After adding your listener and after printing your log, could you print out what that listener's max file size happens to be? I'm curious to see if it indeed retains that threshold. – MSalmo Mar 11 '15 at 05:45
  • Do you mean the value of `MaximumFileSize` property? It's still `1024` at the `ts.TraceData` call. My log file right now is 9867 bytes. – Jeremy Mar 11 '15 at 05:48
  • Oh, I've figured it out. You're actually looking to use `LimitedSequentialFiles` instead of `UnlimitedSequentialFiles`. https://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.tracelogretentionoption%28v=vs.100%29.aspx – MSalmo Mar 11 '15 at 05:51
  • Nup, pretty sure `Unlimited` is the one I'm after. Actually, for whatever reason, it's just started working. I *swear* it wasn't for about 2 hours before I asked the question … but thanks for your help anyway. – Jeremy Mar 11 '15 at 05:57
  • That's strange, but I'm glad it worked out for you in the end. – MSalmo Mar 11 '15 at 05:58
0

OK, short version is that the problem was between the screen and the chair.

There's a few tricks here:

  1. It's really important that the buffer size is less than the maximum file size, which I assumed but never confirmed prior to posting
  2. Depending on how you set up the trace, it seems to miss any requests thrown at it for some amount of time while the application initialises. Not sure why.
  3. Sometimes Windows Explorer doesn't refresh the list of files in a directory. So check that before making a fool of yourself on the internet.
Jeremy
  • 2,642
  • 18
  • 36