C# Class NLog.Targets.FileTarget

Inheritance: NLog.Targets.TargetWithLayoutHeaderAndFooter, ICreateFileParameters
ファイルを表示 Open project: NLog/NLog Class Usage Examples

Private Properties

Property Type Description
ArchiveByDate void
ArchiveByDateAndSequence void
ArchiveBySequence void
ArchiveFile void
AutoClosingTimerCallback void
ContainsFileNamePattern bool
CreateFileNameLayout NLog.Internal.FilePathLayout
DeleteAndWaitForFileDelete void
DeleteOldArchiveFile bool
DeleteOldDateArchives void
DoAutoArchive void
EnsureArchiveCount void
FinalizeFile void
FindDateAndSequenceArchives IEnumerable
FlushCurrentFileWrites void
GetArchiveDate System.DateTime
GetArchiveDateFormatString string
GetArchiveFileName string
GetArchiveFileNameBasedOnFileSize string
GetArchiveFileNameBasedOnTime string
GetArchiveFileNamePattern string
GetFileAppenderFactory IFileAppenderFactory
GetFiles IEnumerable
GetFooterBytes byte[]
GetFullFileName string
GetHeaderBytes byte[]
GetLayoutBytes byte[]
GetPotentialFileForArchiving string
InitializeFile bool
IsArchivingEnabled bool
PreviousLogOverlappedPeriod bool
ProcessLogEvent void
ProcessOnStartup void
RefreshArchiveFilePatternToWatch void
RefreshFileArchive void
ReplaceFileContent void
ReplaceFileNamePattern string
ReplaceNumberPattern string
RollArchivesForward void
ShouldDeleteOldArchives bool
StopAppenderInvalidatorThread void
TryArchiveFile void
TryParseDateAndSequence bool
WriteFooter void
WriteHeader void
WriteToFile void

Public Methods

Method Description
CleanupInitializedFiles ( ) : void

Removes records of initialized files that have not been accessed in the last two days.

Files are marked 'initialized' for the purpose of writing footers when the logging finishes.

CleanupInitializedFiles ( System.DateTime cleanupThreshold ) : void

Removes records of initialized files that have not been accessed after the specified date.

Files are marked 'initialized' for the purpose of writing footers when the logging finishes.

FileTarget ( ) : System

Initializes a new instance of the FileTarget class.

The default value of the layout is: ${longdate}|${level:uppercase=true}|${logger}|${message}

FileTarget ( string name ) : System

Initializes a new instance of the FileTarget class.

The default value of the layout is: ${longdate}|${level:uppercase=true}|${logger}|${message}

Protected Methods

Method Description
CloseTarget ( ) : void

Closes the file(s) opened for writing.

FlushAsync ( AsyncContinuation asyncContinuation ) : void

Flushes all pending file operations.

The timeout parameter is ignored, because file APIs don't provide the needed functionality.

GetBytesToWrite ( LogEventInfo logEvent ) : byte[]

Gets the bytes to be written to the file.

GetFormattedMessage ( LogEventInfo logEvent ) : string

Formats the log event for write.

InitializeTarget ( ) : void

Initializes file logging by creating data structures that enable efficient multi-file logging.

TransformBytes ( byte value ) : byte[]

Modifies the specified byte array before it gets sent to a file.

Write ( NLog.Common.AsyncLogEventInfo logEvents ) : void

Writes the specified array of logging events to a file specified in the FileName parameter.

This function makes use of the fact that the events are batched by sorting the requests by filename. This optimizes the number of open/close calls and can help improve performance.

Write ( LogEventInfo logEvent ) : void

Writes the specified logging event to a file specified in the FileName parameter.

Private Methods

Method Description
ArchiveByDate ( string fileName, string pattern, LogEventInfo logEvent ) : void

Archives the fileName using a date style numbering. Archives will be stamped with the prior period (Year, Month, Day, Hour, Minute) datetime. When the number of archive files exceed the obsolete archives are deleted.

ArchiveByDateAndSequence ( string fileName, string pattern, LogEventInfo logEvent ) : void

Archives the fileName using a date and sequence style numbering. Archives will be stamped with the prior period (Year, Month, Day) datetime. The most recent archive has the highest number (in combination with the date).

When the number of archive files exceed P:MaxArchiveFiles the obsolete archives are deleted.

ArchiveBySequence ( string fileName, string pattern ) : void

Archives the fileName using a sequence style numbering. The most recent archive has the highest number. When the number of archive files exceed P:MaxArchiveFiles the obsolete archives are deleted.

ArchiveFile ( string fileName, string archiveFileName ) : void

Archives fileName to archiveFileName.

AutoClosingTimerCallback ( object state ) : void
ContainsFileNamePattern ( string fileName ) : bool

Determines if the file name as String contains a numeric pattern i.e. {#} in it. Example: trace{#}.log Contains the numeric pattern. trace{###}.log Contains the numeric pattern. trace{#X#}.log Contains the numeric pattern (See remarks). trace.log Does not contain the pattern.

Occasionally, this method can identify the existence of the {#} pattern incorrectly.

CreateFileNameLayout ( Layout value ) : NLog.Internal.FilePathLayout
DeleteAndWaitForFileDelete ( string fileName ) : void
DeleteOldArchiveFile ( string fileName ) : bool
DeleteOldDateArchives ( string pattern ) : void

Deletes archive files in reverse chronological order until only the MaxArchiveFiles number of archive files remain.

DoAutoArchive ( string fileName, LogEventInfo eventInfo ) : void

Invokes the archiving process after determining when and which type of archiving is required.

EnsureArchiveCount ( List oldArchiveFileNames ) : void

Deletes files among a given list, and stops as soon as the remaining files are fewer than the setting.

Items are deleted in the same order as in oldArchiveFileNames. No file is deleted if property is zero.

FinalizeFile ( string fileName, bool isArchiving = false ) : void

Writes the file footer and finalizes the file in FileTarget instance internal structures.

FindDateAndSequenceArchives ( string dirName, string logFileName, string fileNameMask, int minSequenceLength, string dateFormat, FileNameTemplate fileTemplate ) : IEnumerable

Searches a given directory for archives that comply with the current archive pattern.

FlushCurrentFileWrites ( string currentFileName, LogEventInfo firstLogEvent, MemoryStream ms, List pendingContinuations ) : void
GetArchiveDate ( string fileName, LogEventInfo logEvent ) : System.DateTime
GetArchiveDateFormatString ( string defaultFormat ) : string

Gets the correct formatting to be used based on the value of for converting values which will be inserting into file names during archiving. This value will be computed only when a empty value or is passed into defaultFormat

GetArchiveFileName ( string fileName, LogEventInfo ev, int upcomingWriteSize ) : string

Indicates if the automatic archiving process should be executed.

GetArchiveFileNameBasedOnFileSize ( string fileName, int upcomingWriteSize ) : string

Gets the file name for archiving, or null if archiving should not occur based on file size.

GetArchiveFileNameBasedOnTime ( string fileName, LogEventInfo logEvent ) : string

Returns the file name for archiving, or null if archiving should not occur based on date/time.

GetArchiveFileNamePattern ( string fileName, LogEventInfo eventInfo ) : string

Gets the pattern that archive files will match

GetFileAppenderFactory ( ) : IFileAppenderFactory

Returns the suitable appender factory ( IFileAppenderFactory) to be used to generate the file appenders associated with the FileTarget instance. The type of the file appender factory returned depends on the values of various FileTarget properties.

GetFiles ( DirectoryInfo directoryInfo, string fileNameMask ) : IEnumerable

Gets the collection of files in the specified directory which they match the fileNameMask.

GetFooterBytes ( ) : byte[]

The sequence of to be written for the file footer.

GetFullFileName ( LogEventInfo logEvent ) : string

Get full filename (=absolute) and cleaned if needed.

GetHeaderBytes ( ) : byte[]

The sequence of to be written for the file header.

GetLayoutBytes ( Layout layout ) : byte[]

The sequence of to be written in a file after applying any formating and any transformations required from the Layout.

Usually it is used to render the header and hooter of the files.

GetPotentialFileForArchiving ( string fileName ) : string

Returns the correct filename to archive

InitializeFile ( string fileName, LogEventInfo logEvent, bool justData ) : bool

Initialise a file to be used by the FileTarget instance. Based on the number of initialised files and the values of various instance properties clean up and/or archiving processes can be invoked.

IsArchivingEnabled ( ) : bool
PreviousLogOverlappedPeriod ( LogEventInfo logEvent, System.DateTime lastWrite ) : bool
ProcessLogEvent ( LogEventInfo logEvent, string fileName, byte bytesToWrite ) : void
ProcessOnStartup ( string fileName, LogEventInfo logEvent ) : void

Invokes the archiving and clean up of older archive file based on the values of and properties respectively.

RefreshArchiveFilePatternToWatch ( ) : void

Refresh the ArchiveFilePatternToWatch option of the FileAppenderCache. The log file must be watched for archiving when multiple processes are writing to the same open file.

RefreshFileArchive ( ) : void
ReplaceFileContent ( string fileName, byte bytes, bool firstAttempt ) : void

Creates the file specified in fileName and writes the file content in each entirety i.e. Header, Content and Footer.

This method is used when the content of the log file is re-written on every write.

ReplaceFileNamePattern ( string pattern, string replacementValue ) : string

Replaces the string-based pattern i.e. {#} in a file name with the value passed in parameter.

ReplaceNumberPattern ( string pattern, int value ) : string

Replaces the numeric pattern i.e. {#} in a file name with the value parameter value.

RollArchivesForward ( string fileName, string pattern, int archiveNumber ) : void

Archives the fileName using a rolling style numbering (the most recent is always #0 then #1, ..., #N. When the number of archive files exceed P:MaxArchiveFiles the obsolete archives are deleted.

This method is called recursively. This is the reason the archiveNumber is required.

ShouldDeleteOldArchives ( ) : bool

Determine if old archive files should be deleted.

StopAppenderInvalidatorThread ( ) : void
TryArchiveFile ( string fileName, LogEventInfo ev, int upcomingWriteSize ) : void

Archives the file if it should be archived.

TryParseDateAndSequence ( string archiveFileNameWithoutPath, string dateFormat, FileNameTemplate fileTemplate, System.DateTime &date, int &sequence ) : bool

Parse filename with date and sequence pattern

WriteFooter ( string fileName ) : void

Writes the footer information to a file.

WriteHeader ( BaseFileAppender appender ) : void

Writes the header information to a file.

WriteToFile ( string fileName, LogEventInfo logEvent, byte bytes, bool justData ) : void

Evaluates which parts of a file should be written (header, content, footer) based on various properties of FileTarget instance and writes them.

Method Details

CleanupInitializedFiles() public method

Removes records of initialized files that have not been accessed in the last two days.
Files are marked 'initialized' for the purpose of writing footers when the logging finishes.
public CleanupInitializedFiles ( ) : void
return void

CleanupInitializedFiles() public method

Removes records of initialized files that have not been accessed after the specified date.
Files are marked 'initialized' for the purpose of writing footers when the logging finishes.
public CleanupInitializedFiles ( System.DateTime cleanupThreshold ) : void
cleanupThreshold System.DateTime The cleanup threshold.
return void

CloseTarget() protected method

Closes the file(s) opened for writing.
protected CloseTarget ( ) : void
return void

FileTarget() public method

Initializes a new instance of the FileTarget class.
The default value of the layout is: ${longdate}|${level:uppercase=true}|${logger}|${message}
public FileTarget ( ) : System
return System

FileTarget() public method

Initializes a new instance of the FileTarget class.
The default value of the layout is: ${longdate}|${level:uppercase=true}|${logger}|${message}
public FileTarget ( string name ) : System
name string Name of the target.
return System

FlushAsync() protected method

Flushes all pending file operations.
The timeout parameter is ignored, because file APIs don't provide the needed functionality.
protected FlushAsync ( AsyncContinuation asyncContinuation ) : void
asyncContinuation AsyncContinuation The asynchronous continuation.
return void

GetBytesToWrite() protected method

Gets the bytes to be written to the file.
protected GetBytesToWrite ( LogEventInfo logEvent ) : byte[]
logEvent LogEventInfo Log event.
return byte[]

GetFormattedMessage() protected method

Formats the log event for write.
protected GetFormattedMessage ( LogEventInfo logEvent ) : string
logEvent LogEventInfo The log event to be formatted.
return string

InitializeTarget() protected method

Initializes file logging by creating data structures that enable efficient multi-file logging.
protected InitializeTarget ( ) : void
return void

TransformBytes() protected method

Modifies the specified byte array before it gets sent to a file.
protected TransformBytes ( byte value ) : byte[]
value byte The byte array.
return byte[]

Write() protected method

Writes the specified array of logging events to a file specified in the FileName parameter.
This function makes use of the fact that the events are batched by sorting the requests by filename. This optimizes the number of open/close calls and can help improve performance.
protected Write ( NLog.Common.AsyncLogEventInfo logEvents ) : void
logEvents NLog.Common.AsyncLogEventInfo An array of objects.
return void

Write() protected method

Writes the specified logging event to a file specified in the FileName parameter.
protected Write ( LogEventInfo logEvent ) : void
logEvent LogEventInfo The logging event.
return void