- Notifications
You must be signed in to change notification settings - Fork1.4k
How to write a custom target
It’s really easy:
- Create a class that inherits from
NLog.Targets.TargetWithLayout - Override the
Write()method. - In the body of the method invoke
this.Layout.Render()to get the message text, then send the text to the destination media.
See also the updatedHow to write a custom target for structured logging
See also the updatedHow to write a custom async target
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}}}
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)
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.
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
- All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json