-
Notifications
You must be signed in to change notification settings - Fork 1.4k
CsvLayout
A specialized layout that renders CSV-formatted events.
Platforms Supported: All
<targets>
<target>
<layout xsi:type="CsvLayout">
<!-- Layout Options -->
<layout xsi:type="layoutType">Layout</layout>
<footer xsi:type="layoutType">Layout</footer>
<header xsi:type="layoutType">Layout</header>
<!-- CSV Options -->
<quoting>Enum</quoting>
<quoteChar>String</quoteChar>
<withHeader>Boolean</withHeader>
<customColumnDelimiter>String</customColumnDelimiter>
<delimiter>Enum</delimiter>
<column layout="Layout" name="String"/> <!-- repeated -->
</layout>
</target>
</targets>
-
layout - Body layout (can be repeated multiple times). Layout
-
footer - Footer layout. Layout
-
header - Header layout. Layout
-
quoting - Default Quoting mode for columns to ensure valid CSV output. Default: Auto
Possible values:- Auto - Only add quotes when detecting value contains the quote symbol, the separator or newlines (Slow)
- All - Add quotes for all values. Useful for data known to be multiline such as Exception-ToString (Fast)
- Nothing - Quote nothing, but make sure not to include newlines or quote-delimiter in output (Very Fast)
-
quoteChar - Quote Character. Default:
"
-
withHeader - Indicates whether CSV should include header.
Boolean
. Defaulttrue
-
customColumnDelimiter - Custom column delimiter value (valid when
delimiter
is set toCustom
). -
delimiter - Column delimiter. Default: Auto
Possible values:- Auto - Automatically detect from regional settings.
- Comma - Comma
,
character (ASCII 44). - Custom - Custom string, specified by the
CustomColumnDelimiter
. - Pipe - Pipe
|
character (ASCII 124). - Semicolon - Semicolon
;
character (ASCII 59). - Space - Space character (ASCII 32).
- Tab - Tab character (ASCII 9).
-
columns - The array of parameters to be passed.Collection
Each collection item is represented by<column />
element with the following attributes:- layout - Layout of the column.Layout Required.
- name - Name of the column.
-
quoting - Column specific override of the default column quoting (Ex. for column with multiline exception-outpu)
Introduced with NLog 4.6
<target xsi:type="File" name="csvFileExample" fileName="./CsvLogExample.csv">
<layout xsi:type="CsvLayout" delimiter="Tab" withHeader="false">
<column name="time" layout="${longdate}" />
<column name="level" layout="${level:upperCase=true}"/>
<column name="message" layout="${message}" />
<column name="exception" layout="${exception:format=ToString}"/>
<column name="property1" layout="${event-properties:property1}"/>
</layout>
</target>
NLog 4.6 introduces some performance optimizations for the CsvLayout. Reducing memory allocation by better buffer reuse. It is also possible to reduce the overhead of the automatic quoting logic for individual columns:
This is example will greatly reduce the overhead of default Auto-quoting, because of the override for the individual columns:
<layout xsi:type="CsvLayout" delimiter="Tab" withHeader="false">
<column name="time" layout="${longdate}" quoting="Nothing" />
<column name="level" layout="${level:upperCase=true}" quoting="Nothing"/>
<column name="message" layout="${message}" quoting="All" />
<column name="exception" layout="${exception:format=ToString}" quoting="All"/>
<column name="property1" layout="${event-properties:property1}"/>
</layout>
Layout = new CsvLayout()
{
Columns =
{
new CsvColumn("time", "${longdate}"),
new CsvColumn("level", "${level:upperCase=true}"),
new CsvColumn("message", "${message}"),
new CsvColumn("exception", "${exception:format=ToString}"),
new CsvColumn("property1", "${event-properties:property1}"),
}
}
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json