FileLogTraceListener Class

 

Provides a simple listener that directs logging output to file.

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

System.Object
  System.MarshalByRefObject
    System.Diagnostics.TraceListener
      Microsoft.VisualBasic.Logging.FileLogTraceListener

[ComVisibleAttribute(false)]
public class FileLogTraceListener : TraceListener

NameDescription
System_CAPS_pubmethodFileLogTraceListener()

Initializes a new instance of the FileLogTraceListener class with the default name.

System_CAPS_pubmethodFileLogTraceListener(String)

Initializes a new instance of the FileLogTraceListener class with the supplied name.

NameDescription
System_CAPS_pubpropertyAppend

Determines whether to append the output to the current file or write it to a new file.

System_CAPS_pubpropertyAttributes

Gets the custom trace listener attributes defined in the application configuration file.(Inherited from TraceListener.)

System_CAPS_pubpropertyAutoFlush

Indicates whether or not the writing to the log file stream flushes the buffer.

System_CAPS_pubpropertyBaseFileName

Gets or sets the base name for the log files, which is used to create the full log-file name.

System_CAPS_pubpropertyCustomLocation

Gets or sets the log file directory when the Location property is set to Custom.

System_CAPS_pubpropertyDelimiter

Gets or sets the delimiter used to delimit fields within a log message.

System_CAPS_pubpropertyDiskSpaceExhaustedBehavior

Determines what to do when writing to the log file and there is less free disk space available than specified by the ReserveDiskSpace property.

System_CAPS_pubpropertyEncoding

Gets or sets the encoding to use when creating a new log file.

System_CAPS_pubpropertyFilter

Gets and sets the trace filter for the trace listener.(Inherited from TraceListener.)

System_CAPS_pubpropertyFullLogFileName

Gets the current full log-file name.

System_CAPS_pubpropertyIncludeHostName

Indicates whether or not the host name of the logging machine should be included in the output.

System_CAPS_pubpropertyIndentLevel

Gets or sets the indent level.(Inherited from TraceListener.)

System_CAPS_pubpropertyIndentSize

Gets or sets the number of spaces in an indent.(Inherited from TraceListener.)

System_CAPS_pubpropertyIsThreadSafe

Gets a value indicating whether the trace listener is thread safe. (Inherited from TraceListener.)

System_CAPS_pubpropertyLocation

Gets or sets location for the log files.

System_CAPS_pubpropertyLogFileCreationSchedule

Determines which date to include in the names of the log files.

System_CAPS_pubpropertyMaxFileSize

Gets or sets the maximum allowed size of the log file, in bytes.

System_CAPS_pubpropertyName

Gets or sets a name for this TraceListener.(Inherited from TraceListener.)

System_CAPS_protpropertyNeedIndent

Gets or sets a value indicating whether to indent the output.(Inherited from TraceListener.)

System_CAPS_pubpropertyReserveDiskSpace

Gets or sets the amount of free disk space, in bytes, necessary before messages can be written to the log file.

System_CAPS_pubpropertyTraceOutputOptions

Gets or sets the trace output options.(Inherited from TraceListener.)

NameDescription
System_CAPS_pubmethodClose()

Closes the underlying stream for the current log file and releases any resources associated with the current stream.(Overrides TraceListener.Close().)

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the TraceListener.(Inherited from TraceListener.)

System_CAPS_protmethodDispose(Boolean)

Closes the underlying stream and optionally releases the managed resources.(Overrides TraceListener.Dispose(Boolean).)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodFail(String)

Emits an error message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethodFail(String, String)

Emits an error message and a detailed error message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodFlush()

Flushes the underlying stream that writes to the current log file.(Overrides TraceListener.Flush().)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetSupportedAttributes()

Gets the custom XML configuration attributes supported by the trace listener.(Overrides TraceListener.GetSupportedAttributes().)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodTraceData(TraceEventCache, String, TraceEventType, Int32, Object)

Writes trace information, a data object, and event information to the output file or stream.(Overrides TraceListener.TraceData(TraceEventCache, String, TraceEventType, Int32, Object).)

System_CAPS_pubmethodTraceData(TraceEventCache, String, TraceEventType, Int32, Object[])

Writes trace information, an array of data objects, and event information to the output file or stream.(Overrides TraceListener.TraceData(TraceEventCache, String, TraceEventType, Int32, Object[]).)

System_CAPS_pubmethodTraceEvent(TraceEventCache, String, TraceEventType, Int32)

Writes trace and event information to the listener specific output.(Inherited from TraceListener.)

System_CAPS_pubmethodTraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

Writes trace information, a message and event information to the output file or stream.(Overrides TraceListener.TraceEvent(TraceEventCache, String, TraceEventType, Int32, String).)

System_CAPS_pubmethodTraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])

Writes trace information, a formatted array of objects, and event information to the output file or stream.(Overrides TraceListener.TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[]).)

System_CAPS_pubmethodTraceTransfer(TraceEventCache, String, Int32, String, Guid)

Writes trace information, a message, a related activity identity and event information to the listener specific output.(Inherited from TraceListener.)

System_CAPS_pubmethodWrite(Object)

Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethodWrite(Object, String)

Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethodWrite(String)

Writes a verbatim message to disk, without any additional context information.(Overrides TraceListener.Write(String).)

System_CAPS_pubmethodWrite(String, String)

Writes a category name and a message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_protmethodWriteIndent()

Writes the indent to the listener you create when you implement this class, and resets the NeedIndent property to false.(Inherited from TraceListener.)

System_CAPS_pubmethodWriteLine(Object)

Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class, followed by a line terminator.(Inherited from TraceListener.)

System_CAPS_pubmethodWriteLine(Object, String)

Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class, followed by a line terminator.(Inherited from TraceListener.)

System_CAPS_pubmethodWriteLine(String)

Writes a verbatim message to disk, followed by the current line terminator, without any additional context information.(Overrides TraceListener.WriteLine(String).)

System_CAPS_pubmethodWriteLine(String, String)

Writes a category name and a message to the listener you create when you implement the TraceListener class, followed by a line terminator.(Inherited from 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 (Visual Basic).

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.

<configuration>
    <system.diagnostics>
        <sharedListeners>
            <add name="FileLog"
                type="Microsoft.VisualBasic.Logging.FileLogTraceListener, 
                      Microsoft.VisualBasic, Version=8.0.0.0, 
                      Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, 
                      processorArchitecture=MSIL" 
                initializeData="FileLogWriter"/>
        </sharedListeners>
    </system.diagnostics>
</configuration>

.NET Framework
Available since 2.0

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

Return to top
Show: