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

How to write a custom target

Rolf Kristensen edited this pageMay 11, 2025 ·34 revisions

It’s really easy:

  1. Create a class that inherits fromNLog.Targets.TargetWithLayout
  2. Override theWrite() method.
  3. In the body of the method invokethis.Layout.Render() to get the message text, then send the text to the destination media.

⚠️ Don't forget toregister your custom component when loading NLog config!

See also the updatedHow to write a custom target for structured logging

See also the updatedHow to write a custom async target

Example

This is a skeleton target that writes messages to the specified host:

usingNLog;usingNLog.Config;usingNLog.Targets;namespaceMyNamespace{[Target("MyFirst")]publicsealedclassMyFirstTarget:TargetWithLayout{publicLayoutHost{get;set;}="localhost";protectedoverridevoidWrite(LogEventInfologEvent){stringlogMessage=RenderLogEvent(this.Layout,logEvent);stringhostName=RenderLogEvent(this.Host,logEvent);SendTheMessageToRemoteHost(hostName,logMessage);}privatevoidSendTheMessageToRemoteHost(stringhostName,stringmessage){// TODO - write me}}}

How to pass configuration options to the target?

In the example above then one can see how theHost-property is used. Having a public property that sets the required configuration parameters is enough for NLog to use it.

Each attribute that you put in the<target /> definition gets passed to the appropriate public property. NLog takes care of the appropriate conversions necessary so that you can use integer, string, datetime, boolean parameters. Check alsoProperties-constraints-for-custom-extensions

It is recommended to use NLogLayouts or NLogLayout<T> for NLog Target Configuration options, as it will allow users to provide configuration-values from NLog LayoutRenderers (Ex. fromappsettings.json,app.config or environment-variables)

How to handle object lifetime

When needing to create interfaces to external framework or start background-threads, then it is recommended to overrideInitializeTarget and close/release resources again inCloseTarget.

namespaceMyNamespace{[Target("MyFirst")]publicsealedclassMyFirstTarget:TargetWithLayout{protectedoverridevoidInitializeTarget(){base.InitializeTarget();// TODO Custom Init Logic}protectedoverridevoidCloseTarget(){// TODO Custom Close Logicbase.CloseTarget();}}}

It is possible for the same target object to be initialized and closed many times during the lifetime of the application. It is recommended not to use the constructor to start background-threads or initialize interfaces, but merely focus on setting up initial state without starting anything. It is possible to override the Dispose-method, but this method will only be called at the end of application-lifetime.

Multiple type-alias attributes

NLog 5.0 enables you to have multiple type-aliases for a single class. Before one had to inherit from the same class to provide additional type-aliases.

[Target("MyFirst")][Target("MyInitial")]publicsealedclassMyFirstTarget:TargetWithLayout{

The type-alias can then be used when wanting to use the target in the NLog Configuration File. Ex.<target type="MyFirst" name="..." />

Notice NLog 5.0 automatically ignores dashes- in type-aliases, so no extra alias is needed for this: Ex.<target type="My-First" name="..." />

-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