Last updated: 2006-07-26

This website is based on NLog v1.0-RC1. Click here to view the documentation for other versions.

CSVFile Target


File target that can produce *.csv (Comma Separated Values) files. This is an extension to the File Target.

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.Targets.CsvFileTarget

Parameters (blue fields are required):

NameTypeDescription
fileNamestring  ${}
The name of the file to write to.
namestring
The name of the target.
archiveAboveSizeInt64
Automatically archive log files that exceed the specified size in bytes.
archiveEveryArchiveEveryMode
Automatically archive log files every time the specified time passes. Possible options are: year, month, day, hour, minute. Files are moved to the archive as part of the write operation if the current period of time changes. For example if the current hour changes from 10 to 11, the first write that will occur on or after 11:00 will trigger the archiving.

Possible values are:

  • None - Don't archive based on time.
  • Year - Archive every year.
  • Month - Archive every month.
  • Day - Archive daily.
  • Hour - Archive every hour.
  • Minute - Archive every minute.

archiveFileNamestring  ${}
The name of the file to be used for an archive. It may contain a special placeholder {#####} that will be replaced with a sequence of numbers depending on the archiving strategy. The number of hash characters used determines the number of numerical digits to be used for numbering files.
archiveNumberingArchiveNumberingMode
Determines the way file archives are numbered.

Possible values are:

  • Sequence - Sequence style numbering. The most recent archive has the highest number.
  • Rolling - Rolling style numbering (the most recent is always #0 then #1, ..., #N

autoFlushboolean
Automatically flush the file buffers after each log message.

Default value is: True.

bufferSizeinteger
Log file buffer size in bytes.

Default value is: 32768.

concurrentWriteAttemptDelayinteger
The delay in milliseconds to wait before attempting to write to the file again.

Default value is: 1.

concurrentWriteAttemptsinteger
The number of times the write is appended on the file before NLog discards the log message.

Default value is: 10.

concurrentWritesboolean
Enables concurrent writes to the log file by multiple processes on the same host.

Default value is: True.

createDirsboolean
Create directories if they don't exist.

Default value is: True.

customColumnDelimiterstring
Custom column delimiter value (valid when ColumnDelimiter is set to 'Custom')
deleteOldFileOnStartupboolean
Delete old log file on startup.

Default value is: False.

delimiterColumnDelimiterMode
Column delimiter.

Default value is: Auto.

Possible values are:

  • Auto - Automatically detect from regional settings.
  • Comma - Comma (ASCII 44)
  • Semicolon - Semicolon (ASCII 59)
  • Tab - Tab character (ASCII 9)
  • Pipe - Pipe character (ASCII 124)
  • Space - Space character (ASCII 32)
  • Custom - Custom string, specified by the CustomDelimiter

enableFileDeleteboolean
Enable log file(s) to be deleted.

Default value is: True.

encodingstring
File encoding.
fileAttributesWin32FileAttributes
File attributes (Windows only).

Possible values are:

  • Readonly - Read-only
  • Hidden - Hidden
  • System - System
  • Archive - File should be archived.
  • Device - Device
  • Normal - Normal
  • Temporary - File is temporary (should be kept in cache and not written to disk if possible)
  • SparseFile - Sparse file.
  • ReparsePoint - Reparse point.
  • Compressed - Compress file contents.
  • NotContentIndexed - File should not be indexed by the content indexing service.
  • Encrypted - Encrypt file.
  • WriteThrough - The system writes through any intermediate cache and goes directly to disk.
  • NoBuffering - The system opens a file with no system caching
  • DeleteOnClose - Delete file after it is closed.
  • PosixSemantics - A file is accessed according to POSIX rules.

keepFileOpenboolean
Keep log file open instead of opening and closing it on each logging event.

Default value is: True.

lineEndingLineEndingMode
Line ending mode.

Possible values are:

  • Default - Insert platform-dependent end-of-line sequence after each line.
  • CRLF - Insert CR LF sequence (ASCII 13, ASCII 10) after each line.
  • CR - Insert CR character (ASCII 13) after each line.
  • LF - Insert LF character (ASCII 10) after each line.
  • None - Don't insert any line ending.

maxArchiveFilesinteger
Maximum number of archive files that should be kept.

Default value is: 9.

networkWritesboolean
Disables open-fi

Default value is: False.

openFileCacheSizeinteger
The number of files to be kept open. Setting this to a higher value may improve performance in a situation where a single File target is writing to many files (such as splitting by level or by logger).

Default value is: 5.

openFileCacheTimeoutinteger
Maximum number of seconds that files are kept open.

Default value is: 1.

quoteCharstring
Quote Character

Default value is: ".

quotingCsvQuotingMode
Quoting mode.

Possible values are:

  • All - Quote all column.
  • Nothing - Quote nothing.
  • Auto - Quote only whose values contain the quote symbol or the separator.

replaceFileContentsOnEachWriteboolean
Replace file contents on each write instead of appending log message at the end.

Default value is: False.

Columns Collection of CsvFileColumn. Each element is represented as <column/>
NameTypeDescription
namestring
The name of the column.
layoutstring
The layout that should be written in the column.

Example:

To set up the target in the configuration file, use the following syntax:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="csv" xsi:type="CSVFile" fileName="${basedir}/file.csv"> 
      <column name="time" layout="${longdate}"/> 
      <column name="message" layout="${message}"/> 
      <column name="logger" layout="${logger}"/> 
      <column name="level" layout="${level}"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="csv"/> 
  </rules> 
</nlog> 

This assumes just one target and a single rule. More configuration options are described here.

To set up the log target programmatically use code like this:

using System; 
 
using NLog; 
using NLog.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        CsvFileTarget target = new CsvFileTarget(); 
        target.FileName = "${basedir}/file.csv"; 
 
        target.Columns.Add(new CsvFileColumn("time", "${longdate}")); 
        target.Columns.Add(new CsvFileColumn("message", "${message}")); 
        target.Columns.Add(new CsvFileColumn("logger", "${logger}")); 
        target.Columns.Add(new CsvFileColumn("level", "${level}")); 
 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Debug("log message"); 
        logger.Debug("Message with \"quotes\" and \nnew line characters."); 
    } 
}

More examples can be found in File Target reference.


Back to the target list.
Webwww.nlog-project.org