-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Properties constraints for custom extensions
Rolf Kristensen edited this page Nov 27, 2021
·
6 revisions
Custom Targets, layout etc. could have properties. When configuring from the XML config, there are some limitations on the types.
Supported types for Targets, Layouts and Layout renderers:
- .NET primitive types: e.g. bool, char, decimal, double, float, int, uint, etc.
- Enums (use short name in the XML)
Encoding
CultureInfo
Type
LineEndingMode
Uri
- NLog types:
Layout
,SimpleLayout
&ConditionExpression
- Types which has an implicit conversion from
string
- Types which are using
TypeDescriptor
fromstring
- Collections, introduced in NLog 4.4. See section Collection types
Introduced in NLog 4.4, collection types could be used.
Usage in XML: comma separated string. If the value contains a comma, single quote the whole value.
Examples:
value="one arg"
value="1,2"
value="value1,'value2, with comma'"
Supported types:
-
IList<T>
/IList
-
IEnumerable<T>
/IEnumerable
-
ISet<T>
/HashSet<T>
with the following types:
- .NET built in types (string, int, double, object)
- enums
- culture, encoding, Type
- not supported: Layout
Not supported:
- Arrays
- Non-generic
List
- Non-gereric
IList
- Custom class implementing/inheriting types listed above (because of performance)
PS: .NET 3.5 hasn't ISet<T>
, so use HashSet<T>
For targets and layout renderers there is also support for XML elements.
E.g. the JSON layout:
<target name="jsonFile" xsi:type="File" fileName="${logFileNamePrefix}.json">
<layout xsi:type="JsonLayout">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
</layout>
</target>
Usage in C#
[ArrayParameter(typeof(JsonAttribute), "attribute")]
public IList<JsonAttribute> Attributes { get; private set; }
[NLogConfigurationItem]
public class JsonAttribute {} //needs default ctor
Another example is the Database target
- 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