Export (0) Print
Expand All

FileLogTraceListener Class

Provides a simple listener that directs logging output to file.

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

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

[ComVisibleAttribute(false)]
public class FileLogTraceListener : TraceListener

The FileLogTraceListener type exposes the following members.

  NameDescription
Public methodFileLogTraceListener()Initializes a new instance of the FileLogTraceListener class with the default name.
Public methodFileLogTraceListener(String)Initializes a new instance of the FileLogTraceListener class with the supplied name.
Top

  NameDescription
Public propertyAppendDetermines whether to append the output to the current file or write it to a new file.
Public propertyAttributesGets the custom trace listener attributes defined in the application configuration file. (Inherited from TraceListener.)
Public propertyAutoFlushIndicates whether or not the writing to the log file stream flushes the buffer.
Public propertyBaseFileNameGets or sets the base name for the log files, which is used to create the full log-file name.
Public propertyCustomLocationGets or sets the log file directory when the Location property is set to Custom.
Public propertyDelimiterGets or sets the delimiter used to delimit fields within a log message.
Public propertyDiskSpaceExhaustedBehaviorDetermines what to do when writing to the log file and there is less free disk space available than specified by the ReserveDiskSpace property.
Public propertyEncodingGets or sets the encoding to use when creating a new log file.
Public propertyFilterGets and sets the trace filter for the trace listener. (Inherited from TraceListener.)
Public propertyFullLogFileNameGets the current full log-file name.
Public propertyIncludeHostNameIndicates whether or not the host name of the logging machine should be included in the output.
Public propertyIndentLevelGets or sets the indent level. (Inherited from TraceListener.)
Public propertyIndentSizeGets or sets the number of spaces in an indent. (Inherited from TraceListener.)
Public propertyIsThreadSafeGets a value indicating whether the trace listener is thread safe. (Inherited from TraceListener.)
Public propertyLocationGets or sets location for the log files.
Public propertyLogFileCreationScheduleDetermines which date to include in the names of the log files.
Public propertyMaxFileSizeGets or sets the maximum allowed size of the log file, in bytes.
Public propertyNameGets or sets a name for this TraceListener. (Inherited from TraceListener.)
Protected propertyNeedIndentGets or sets a value indicating whether to indent the output. (Inherited from TraceListener.)
Public propertyReserveDiskSpaceGets or sets the amount of free disk space, in bytes, necessary before messages can be written to the log file.
Public propertyTraceOutputOptionsGets or sets the trace output options. (Inherited from TraceListener.)
Top

  NameDescription
Public methodCloseCloses the underlying stream for the current log file and releases any resources associated with the current stream. (Overrides TraceListener.Close().)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the TraceListener. (Inherited from TraceListener.)
Protected methodDispose(Boolean)Closes the underlying stream and optionally releases the managed resources. (Overrides TraceListener.Dispose(Boolean).)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodFail(String)Emits an error message to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodFail(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.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFlushFlushes the underlying stream that writes to the current log file. (Overrides TraceListener.Flush().)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetSupportedAttributesGets the custom XML configuration attributes supported by the trace listener. (Overrides TraceListener.GetSupportedAttributes().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodTraceData(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).)
Public methodTraceData(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[]).)
Public methodTraceEvent(TraceEventCache, String, TraceEventType, Int32)Writes trace and event information to the listener specific output. (Inherited from TraceListener.)
Public methodTraceEvent(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).)
Public methodTraceEvent(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[]).)
Public methodTraceTransferWrites trace information, a message, a related activity identity and event information to the listener specific output. (Inherited from TraceListener.)
Public methodWrite(Object)Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodWrite(String)Writes a verbatim message to disk, without any additional context information. (Overrides TraceListener.Write(String).)
Public methodWrite(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.)
Public methodWrite(String, String)Writes a category name and a message to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Protected methodWriteIndentWrites the indent to the listener you create when you implement this class, and resets the NeedIndent property to false. (Inherited from TraceListener.)
Public methodWriteLine(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.)
Public methodWriteLine(String)Writes a verbatim message to disk, followed by the current line terminator, without any additional context information. (Overrides TraceListener.WriteLine(String).)
Public methodWriteLine(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.)
Public methodWriteLine(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.)
Top

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

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft