FileLogTraceListener Class

Definition

Provides a simple listener that directs logging output to file.

public ref class FileLogTraceListener : System::Diagnostics::TraceListener
[System.Runtime.InteropServices.ComVisible(false)]
public class FileLogTraceListener : System.Diagnostics.TraceListener
[<System.Runtime.InteropServices.ComVisible(false)>]
type FileLogTraceListener = class
    inherit TraceListener
Public Class FileLogTraceListener
Inherits TraceListener
Inheritance
FileLogTraceListener
Attributes

Remarks

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.

In .NET Framework apps, you can set the level of your listener by editing 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.

<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>  

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.

Constructors

FileLogTraceListener()

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

FileLogTraceListener(String)

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

Properties

Append

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

Attributes

Gets the custom trace listener attributes defined in the application configuration file.

(Inherited from TraceListener)
AutoFlush

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

BaseFileName

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

CustomLocation

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

Delimiter

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

DiskSpaceExhaustedBehavior

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

Encoding

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

Filter

Gets or sets the trace filter for the trace listener.

(Inherited from TraceListener)
FullLogFileName

Gets the current full log-file name.

IncludeHostName

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

IndentLevel

Gets or sets the indent level.

(Inherited from TraceListener)
IndentSize

Gets or sets the number of spaces in an indent.

(Inherited from TraceListener)
IsThreadSafe

Gets a value indicating whether the trace listener is thread safe.

(Inherited from TraceListener)
Location

Gets or sets location for the log files.

LogFileCreationSchedule

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

MaxFileSize

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

Name

Gets or sets a name for this TraceListener.

(Inherited from TraceListener)
NeedIndent

Gets or sets a value indicating whether to indent the output.

(Inherited from TraceListener)
ReserveDiskSpace

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

TraceOutputOptions

Gets or sets the trace output options.

(Inherited from TraceListener)

Methods

Close()

Closes the underlying stream for the current log file and releases any resources associated with the current stream.

CreateObjRef(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)
Dispose()

Releases all resources used by the TraceListener.

(Inherited from TraceListener)
Dispose(Boolean)

Closes the underlying stream and optionally releases the managed resources.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Fail(String)

Emits an error message to the listener you create when you implement the TraceListener class.

(Inherited from TraceListener)
Fail(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)
Flush()

Flushes the underlying stream that writes to the current log file.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetSupportedAttributes()

Gets the custom XML configuration attributes supported by the trace listener.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToString()

Returns a string that represents the current object.

(Inherited from Object)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object)

Writes trace information, a data object, and event information to the output file or stream.

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

Writes trace information, an array of data objects, and event information to the output file or stream.

TraceEvent(TraceEventCache, String, TraceEventType, Int32)

Writes trace and event information to the listener specific output.

(Inherited from TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

Writes trace information, a message and event information to the output file or stream.

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

Writes trace information, a formatted array of objects, and event information to the output file or stream.

TraceTransfer(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)
Write(Object)

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

(Inherited from TraceListener)
Write(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)
Write(String)

Writes a verbatim message to disk, without any additional context information.

Write(String, String)

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

(Inherited from TraceListener)
WriteIndent()

Writes the indent to the listener you create when you implement this class, and resets the NeedIndent property to false.

(Inherited from TraceListener)
WriteLine(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)
WriteLine(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)
WriteLine(String)

Writes a verbatim message to disk, followed by the current line terminator, without any additional context information.

WriteLine(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)

Applies to

See also