Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/NLogPublic

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

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 fromstring
  • Types which are usingTypeDescriptor fromstring
  • Collections, introduced in NLog 4.4. See sectionCollection types

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-genericList
  • Non-gerericIList
  • Custom class implementing/inheriting types listed above (because of performance)

PS: .NET 3.5 hasn'tISet<T>, so useHashSet<T>

XML Elements

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

Configuration

Programmatic Configuration

Advanced

Extending NLog

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp