NLog output target and type, NLog output target type
Targets: output target node
Target: Configure an output target.
Type output Type:
- Console output to the Console
- Debugger output to VS Output Window
- File output to File
- Mail output is sent by Mail
- Network output to Network address
- Database output to Database
<Targets async = "Boolean"> // whether or not it is asynchronous <target xsi: type = "File" // output type name = "String" // node name layout = "Layout" // log input format header = "Layout" // text displayed in the log header content footer = "Layout" // The text content displayed at the end of the log encoding = "Encoding" // file encoding format lineEnding = "Enum" // Line End mode archiveAboveSize = "Long "/ /archive upper limit maxArchiveFiles = "Integer" // maximum number of archive files saved, the default value is 9 archiveFileName = "Layout" // The Name Of The archive file. Allowed to use Layout archiveNumbering = "Enum" // archiveEvery = "Enum" // whether to automatically archive the log file replaceFileContentsOnEachWrite = "Boolean"/whether overwrite the log Content written each time, default Value: false fileAttributes = "Enum" // File Attribute (only for Windows) fileName = "Layout" // The name of the file to which logs are written supports Layout deleteOldFileOnStartup = "Boolean" // at startup, whether to delete the old log file. The default value is false enableFileDelete = "Boolean" // no log files can be deleted. The default value is true createDirs = "Boolean" // whether to allow automatic (nonexistent) Directory Creation. The default value is true. ConcurrentWrites = "Boolean" // whether multiple processes are allowed to concurrently write log information to the file ., The default value is true openFileCacheTimeout = "Integer" // The maximum number of seconds that the file remains open. openFileCacheSize = "Integer" // The number of files in the open state networkWrites = "Boolean" // whether the file passes multi-thread write logs to files concurrently by different network hosts; default value: false concurrentWriteAttemptDelay = "Integer" concurrentWriteAttempts = "Integer" bufferSize = "Integer" // Log File Cache size (in bytes) the default value is 32768 (32KB) autoFlush = "Boolean" // after each log information, whether to automatically refresh the File Cache. The default value is true keepFileOpen = "Boolean" // whether to keep the log file in the open state, in place of its frequent opening and disabling when each log write event occurs. The default value is fa. Lse. Note: setting this attribute to true helps improve performance. /> </Targets>View Code
Parameter description:
Layout-Text Content of the log. Default Value:
$ {Longdate} | $ {level: uppercase = true} | $ {logger} | $ {message}
LineEnding-Row end mode.
Optional values:
- CR-Insert the carriage return symbol (ASCII 13) at each end ).
- CRLF-Insert the carriage return and line feed characters (ASCII 13 & ASCII 10) at the end of each line ).
- Default-Insert the end symbol of the platform line at the end of each line (default ).
- LF-Insert a line break (ASCII 10) at the end of each line ).
- None-No end symbol is inserted.
Archiveabvesize-Archive upper limit. If the log file is greater than this value, it is automatically archived. Its type is Long.
Note: enabling this option can significantly reduce the logging speed when logs are recorded concurrently in multiple processes. To achieve optimal performance, you can set ConcurrentWrites to false in a single process.
ArchiveEvery-Whether to automatically archive log files at each specified time point.
Optional values:
- Day-Daily archive.
- Hour-Archive every hour.
- Minute-Archive every minute.
- Month-Archive every month.
- None-Fixed archiving by time.
- Year-Archive each year.
Note: As a part of the log writing, the time when the file is moved to the archive location changes. For example, if the current time is changed from to, log file archive is triggered when the first write operation after or is written.
Note: enabling this option can significantly reduce the logging speed when logs are recorded concurrently in multiple processes. To achieve optimal performance, you can set ConcurrentWrites to false in a single process.
Filename-Name of the file to which logs are written. Layout is supported.
Note: The file name string may contain instance variables in the layout Renderer. In this way, you can configure a single target node to write logs into multiple files.
The following fileName attribute values make NLog in the application startup directory and write the log information to different files based on different log levels.
$ {Basedir}/$ {level}. log
Therefore, all Debug-level logs are written to the Debug. log file, and all Info-level logs are written to the Info. log file, and so on. At the same time, you can also combine other layout Renderer instances to form the desired log file name.
DeleteOldFileOnStartUp-Whether to delete the old log file at startup. The value type is Boolean. The default value is false.
Note: This option is only valid when the "fileName" parameter is a single file.
Performance Optimization Options
ConcurrentWirtes-Whether to allow multiple processes to concurrently write log information to files. The value type is Boolean. The default value is true.
Note: This makes it possible for multiple processes to record logs. NLog uses a special technique to keep files open for writing.
OpenFileCacheTimeout-The maximum number of seconds that the file remains open. If this number is negative, the file will not be closed automatically after a certain period of inactivity. The value type is Integer, and the default value is-1. (That is, the file is always in the open state for writing .)
OpenFileCacheSize-The number of files that remain open. When you set a single object type target and write results into multiple different files (for example, according to the log level or log object name, you can set this attribute to a high value to improve performance. The value type is Integer and the default value is 5.
Note: These files are managed using algorithms at least recently. When the cache space is insufficient, these files are flushed for the longest time. In general, you should not set this parameter too large. It is better not to exceed 10-15 because multiple files are kept open at the same time, which is a huge consumption for system resources.
NetworkWrites-Whether to write logs to files concurrently by different network hosts through multithreading. The value type is Boolean, and the default value is false.
Note: This method can effectively prevent files from being saved as open for a long time.
ConcurrentWriteAttemptDelay-The number of milliseconds before the log is written into the file again. The value type is Integer and the default value is 1.
Note: The actual delay time is a random integer between 0 and the specified parameter value. After each attempt fails, the parameter is doubled. If the value of this parameter is 10, a random number between 0 and 10 is the delay (in milliseconds) of the First retry write, and a random number between 0 and 20 is the second retry, A random number between 0 and 40 in the third retry, a random number between 0 and 80 in the fourth retry, and so on.
ConcurrentWriteAttempts-Number of writes before NLog discards the log information. The value type is Integer and the default value is 10.
BufferSize-Log File Cache size (in bytes ). The value type is Integer. The default value is 32768 (32 KB ).
AutoFlush-Whether to automatically refresh the File Cache after each log message. The value type is Boolean, and the default value is true.
KeepFileOpen-Whether to keep the log file open, instead of opening or closing it frequently when each log write event occurs. The value type is Boolean, and the default value is false.
Note: setting this attribute to true helps improve performance.