| Name | Type | Description |
|---|
| fileName | string
${} | | The name of the file to write to. |
|
| name | string | |
| archiveAboveSize | Int64 | | Automatically archive log files that exceed the specified size in bytes. |
|
| archiveEvery | ArchiveEveryMode | 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.
|
|
| archiveFileName | string
${} | | 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. |
|
| archiveNumbering | ArchiveNumberingMode | | 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
|
|
| autoFlush | boolean | | Automatically flush the file buffers after each log message. Default value is: True. |
|
| bufferSize | integer | | Log file buffer size in bytes. Default value is: 32768. |
|
| concurrentWriteAttemptDelay | integer | | The delay in milliseconds to wait before attempting to write to the file again. Default value is: 1. |
|
| concurrentWriteAttempts | integer | | The number of times the write is appended on the file before NLog discards the log message. Default value is: 10. |
|
| concurrentWrites | boolean | | Enables concurrent writes to the log file by multiple processes on the same host. Default value is: True. |
|
| createDirs | boolean | | Create directories if they don't exist. Default value is: True. |
|
| customColumnDelimiter | string | | Custom column delimiter value (valid when ColumnDelimiter is set to 'Custom') |
|
| deleteOldFileOnStartup | boolean | | Delete old log file on startup. Default value is: False. |
|
| delimiter | ColumnDelimiterMode | | 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
|
|
| enableFileDelete | boolean | | Enable log file(s) to be deleted. Default value is: True. |
|
| encoding | string | |
| fileAttributes | Win32FileAttributes | | 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.
|
|
| keepFileOpen | boolean | | Keep log file open instead of opening and closing it on each logging event. Default value is: True. |
|
| lineEnding | LineEndingMode | | 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.
|
|
| maxArchiveFiles | integer | | Maximum number of archive files that should be kept. Default value is: 9. |
|
| networkWrites | boolean | | Disables open-fi Default value is: False. |
|
| openFileCacheSize | integer | | 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. |
|
| openFileCacheTimeout | integer | | Maximum number of seconds that files are kept open. Default value is: 1. |
|
| quoteChar | string | | Quote Character Default value is: ". |
|
| quoting | CsvQuotingMode | | Quoting mode.
Possible values are:
All - Quote all column. Nothing - Quote nothing. Auto - Quote only whose values contain the quote symbol or the separator.
|
|
| replaceFileContentsOnEachWrite | boolean | | 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/>
|
| Name | Type | Description |
|---|
| name | string | | | layout | string | | The layout that should be written in the column. |
|
|
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.