This documentation is archived and is not being maintained.

FileLogTraceListener Class

Provides a simple listener that directs logging output to file.

Namespace:  Microsoft.VisualBasic.Logging
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public class FileLogTraceListener : TraceListener

The FileLogTraceListener class provides automated maintenance capabilities to archive log files as needed, on a daily or per-application basis. This automatic archival functionality helps reduce the maintenance responsibilities of developers and administrators.

An instance of FileLogTraceListener can be added to the Debug.Listeners or Trace.Listeners collections to redirect output from logging to a text file. Instances of this class can also be added to My.Application.Log or My.Log (for Web applications) in Visual Basic applications. For more information, see Walkthrough: Changing Where My.Application.Log Writes Information.

The main features of this class are:

  1. Archival functionality. The log files generated by this class are named according to the base name and the date, along with a number to distinguish the log file from successive versions of the log. New log files are created on an as-needed basis.

    The explicit form of the file name is baseName[-dateStamp][-version].log, where:

    • The baseName part is the fundamental log name, specified by the BaseFileName property.

    • The dateStamp part has the format "YYYY-MM-DD", and it is shown when LogFileCreationSchedule is Daily or Weekly.

    • If more than one log file is needed with the same baseName and dateStamp, the version part, a positive Integer, is added to the file name.

  2. Multiple class instances. If an instance of the FileLogTraceListener class writes to a file that is in use:

    • The class shares the file if it is being used by another instance of the FileLogTraceListener class in the same process.

    • The class creates a new log file using the next available name if the file is being used by another process.

  3. Thread safety. The FileLogTraceListener class is thread safe. This allows you to safely write messages to the log from multiple threads without using locks.

Log-File Location

  • The Location property takes a LogFileLocation enumeration to specify one of the typical directories to write the log file to.

  • To write the log to another location, set the CustomLocation property to that location.

Log-File Name

  • The base name for the log file is specified by the BaseFileName property.

  • The current log file name can be read from the FullLogFileName property. It is derived from several other properties and the current state of the logs in the file system.

Log Maintenance

  • The minimum frequency for creating new log files is determined by the LogFileCreationSchedule property. When the value is Daily or Weekly, a new log file is created at least once every day or week, and a date stamp is incorporated into the FullLogFileName name.

  • The maximum size (in bytes) of the log file is determined by the MaxFileSize property. If the log file size exceeds this size, additional messages written to the log are discarded and, depending on the DiskSpaceExhaustedBehavior property, an exception is thrown.

  • The ReserveDiskSpace property determines how much free space (in bytes) must be available. This helps ensure that the FileLogTraceListener class will not consume all available disk space. Use the DiskSpaceExhaustedBehavior property to specify the behavior of log writes when there are less than ReserveDiskSpace bytes free.

Log-File Output Settings

  • The AutoFlush property specifies whether the underlying stream should be flushed automatically after each write to the log file.

  • The Append property specifies whether to append messages to the current log file, if allowed, or write them to a new log file.

  • The Delimiter property specifies the string to use for delimiting the fields within a log message.

  • The Encoding property specifies the encoding to use when creating a new log file.

Log-File Output

  • The TraceData and TraceEvent methods write messages to the log file. These methods check the LogFileCreationSchedule property, along with any existing logs with the same base name to determine if a new log should be created.

  • The Flush method flushes any messages currently in the output buffer to the log file.

  • The Close method closes the log file so that it can be used by other processes.

Enabling Output

You must enable tracing or debugging to use a trace listener. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.

  • To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.

  • To enable tracing in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

To set the level of your listener, edit the configuration file for your application. Within this file, you can add a listener, set its type and set its parameters, remove a listener, or clear all the listeners previously set by the application. The configuration file should be formatted like the following example.

For this example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

            <add name="FileLog"
                      Microsoft.VisualBasic, Version=, 
                      Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, 


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0