FileLogTraceListener Class

Writes to a rolling text file.

Remarks

A new file is used when the maxFileSize is reached, as well as a daily or weekly
basis as specified by logFileCreationSchedule.

Each file has a name in the format "<directory>\<baseFileName>(-<yyyy-MM-dd>)(-<seq>).log",
with the local date included for daily and weekly rotation, and a sequence number
appended if the file already exists.

Directory is determined by the location enumeration, and customLocation (if using
a custom location).

Trace information is written to the file with Source, EventType, Id, and Message,
separated by the specified delimiter.

If TraceData() is used, instead of TraceEvent(), then all Data items are written,
each separated by the delimiter, instead of Message (the number of delimiters will
vary based on the number of data items).

Depending on traceOutputOptions the Callstack, LogicalOperationStack,
DateTime (UTC), ProcessId, ThreadId, and Timestamp are then written, separated
by the delimiter. If the includeHostName option is set, then a final delimiter and
the MachineName are also written.

For an alternative that allows a customised output format and alternative creation
frequencies, see RollingFileTraceListener.

Config Attributes

Attribute Description
initalizeData Ignored.
traceOutputOptions Appended to output, separated by the specified delimiter.
baseFileName Default is the application executable name.
customLocation Applies if location is Custom; default is Application.UserAppDataPath.
delimiter Default is tab delimited (in XML this is "&#x9;").
encoding Default is UTF8, but may use any named encoding supported by Encoding.GetEncoding().
logFileCreationSchedule Rotates logs on daily or weekly basis by appending the date.

Example Config

Note: You may need to change the version number of Visual Basic based on the .NET version you are using.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sharedListeners>
      <add name="filelog"
        type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        traceOutputOptions="DateTime,ProcessId,ThreadId"
        customLocation="Logs"
        location="Custom"
        logFileCreationSchedule="Daily" />
    </sharedListeners>
    <sources>
      <source name="ExampleSource" switchValue="All">
        <listeners>
          <clear />
          <add name="filelog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

Example Output

Download the Complete package with examples and see the FileLog subfolder in the HelloLogging example.

Config Template

<!-- Note: You may need to change the version number of Visual Basic based on the .NET version you are using. -->
<add name="filelog"
  type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
  initalizeData=""
  traceOutputOptions="Callstack,LogicalOperationStack,DateTime,ProcessId,ThreadId,Timestamp,"
  append="true|false"
  autoFlush="false|true"
  baseFileName=""
  customLocation=""
  delimiter="&#x9;"
  diskSpaceExhaustedBehavior="DiscardMessages|ThrowException"
  encoding="UTF8|encoding name"
  includeHostName="false|true"
  location="LocalUserApplicationDirectory|TempDirectory|CommonApplicationDirectory|ExecutableDirectory|Custom"
  logFileCreationSchedule="None|Daily|Weekly"
  maxFileSize="5000000"
  reserveDiskSpace="10000000"
  />

Last edited Mar 27, 2013 at 12:02 AM by sgryphon, version 1

Comments

mheyman Feb 2, 2015 at 1:28 PM 
The text about MaxFileSize is incorrect, no new file gets used when that limit is reached. The FileLogTraceListener docs state about MaxFileSize:

If the log file size (in bytes) is greater than the value of this property [MaxFileSize], then when the FileLogTraceListener class attempts to write a message to the log, the message is discarded and the failure is either silent or an exception is thrown depending on the value of the DiskSpaceExhaustedBehavior property. Otherwise, the message is written to the log, if the amount of free disk space (in bytes) is less than ReserveDiskSpace.