File Trace Listener Log
File Trace Listener Log
File Trace Listener Log
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
type FileLogTraceListener = class inherit TraceListener
Public Class FileLogTraceListener Inherits 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.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:
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
baseNamepart is the fundamental log name, specified by the BaseFileName property.
If more than one log file is needed with the same
versionpart, a positive
Integer, is added to the file name.
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.
Thread safety. The FileLogTraceListener class is thread safe. This allows you to safely write messages to the log from multiple threads without using locks.
To write the log to another location, set the CustomLocation property to that location.
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.
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.
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.
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:DEBUGflag to the compiler command line when you compile your code, or you can add
#define DEBUGto the top of your file. In Visual Basic, add the
/d:DEBUG=Trueflag to the compiler command line.
To enable tracing in C#, add the
/d:TRACEflag to the compiler command line when you compile your code, or add
#define TRACEto the top of your file. In Visual Basic, add the
/d:TRACE=Trueflag 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=18.104.22.168, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter"/> </sharedListeners> </system.diagnostics> </configuration>
|FileLogTraceListener() FileLogTraceListener() FileLogTraceListener() FileLogTraceListener()||
Initializes a new instance of the FileLogTraceListener class with the default name.
|FileLogTraceListener(String) FileLogTraceListener(String) FileLogTraceListener(String) FileLogTraceListener(String)||
Initializes a new instance of the FileLogTraceListener class with the supplied name.
|Append Append Append Append||
Determines whether to append the output to the current file or write it to a new file.
|Attributes Attributes Attributes Attributes||
Gets the custom trace listener attributes defined in the application configuration file.(Inherited from TraceListener)
|AutoFlush AutoFlush AutoFlush AutoFlush||
Indicates whether or not the writing to the log file stream flushes the buffer.
|BaseFileName BaseFileName BaseFileName BaseFileName||
Gets or sets the base name for the log files, which is used to create the full log-file name.
|CustomLocation CustomLocation CustomLocation CustomLocation|
|Delimiter Delimiter Delimiter Delimiter||
Gets or sets the delimiter used to delimit fields within a log message.
|DiskSpaceExhaustedBehavior DiskSpaceExhaustedBehavior DiskSpaceExhaustedBehavior 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 Encoding Encoding Encoding||
Gets or sets the encoding to use when creating a new log file.
|Filter Filter Filter Filter||
Gets or sets the trace filter for the trace listener.(Inherited from TraceListener)
|FullLogFileName FullLogFileName FullLogFileName FullLogFileName||
Gets the current full log-file name.
|IncludeHostName IncludeHostName IncludeHostName IncludeHostName||
Indicates whether or not the host name of the logging machine should be included in the output.
|IndentLevel IndentLevel IndentLevel IndentLevel||
Gets or sets the indent level.(Inherited from TraceListener)
|IndentSize IndentSize IndentSize IndentSize||
Gets or sets the number of spaces in an indent.(Inherited from TraceListener)
|IsThreadSafe IsThreadSafe IsThreadSafe IsThreadSafe||
Gets a value indicating whether the trace listener is thread safe.(Inherited from TraceListener)
|Location Location Location Location||
Gets or sets location for the log files.
|LogFileCreationSchedule LogFileCreationSchedule LogFileCreationSchedule LogFileCreationSchedule||
Determines which date to include in the names of the log files.
|MaxFileSize MaxFileSize MaxFileSize MaxFileSize||
Gets or sets the maximum allowed size of the log file, in bytes.
|Name Name Name Name||
Gets or sets a name for this TraceListener.(Inherited from TraceListener)
|NeedIndent NeedIndent NeedIndent NeedIndent||
Gets or sets a value indicating whether to indent the output.(Inherited from TraceListener)
|ReserveDiskSpace ReserveDiskSpace ReserveDiskSpace ReserveDiskSpace||
Gets or sets the amount of free disk space, in bytes, necessary before messages can be written to the log file.
|TraceOutputOptions TraceOutputOptions TraceOutputOptions TraceOutputOptions||
Gets or sets the trace output options.(Inherited from TraceListener)
- BaseFileName BaseFileName BaseFileName BaseFileName
- TraceData(TraceEventCache, String, TraceEventType, Int32, Object) TraceData(TraceEventCache, String, TraceEventType, Int32, Object) TraceData(TraceEventCache, String, TraceEventType, Int32, Object) TraceData(TraceEventCache, String, TraceEventType, Int32, Object)
- TraceEvent(TraceEventCache, String, TraceEventType, Int32, String) TraceEvent(TraceEventCache, String, TraceEventType, Int32, String) TraceEvent(TraceEventCache, String, TraceEventType, Int32, String) TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)
- Flush() Flush() Flush() Flush()
- Close() Close() Close() Close()
- Walkthrough: Changing Where My.Application.Log Writes Information
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.