- Notifications
You must be signed in to change notification settings - Fork1.4k
Properties constraints for custom extensions
Rolf Kristensen edited this pageNov 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)
EncodingCultureInfoTypeLineEndingModeUri- NLog types:
Layout,SimpleLayout&ConditionExpression - Types which has an implicit conversion from
string - Types which are using
TypeDescriptorfromstring - Collections, introduced in NLog 4.4. See sectionCollection 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>/IListIEnumerable<T>/IEnumerableISet<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'tISet<T>, so useHashSet<T>
For targets and layout renderers there is also support for XML elements.
E.g. theJSON layout:
<targetname="jsonFile"xsi:type="File"fileName="${logFileNamePrefix}.json"> <layoutxsi:type="JsonLayout"> <attributename="time"layout="${longdate}" /> <attributename="level"layout="${level:upperCase=true}"/> <attributename="message"layout="${message}" /> </layout></target>
Usage in C#
[ArrayParameter(typeof(JsonAttribute),"attribute")]publicIList<JsonAttribute>Attributes{get; privateset;}[NLogConfigurationItem]publicclassJsonAttribute{}//needs default ctor
Another example is theDatabase target
-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config
- All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json