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 for structured logging

Rolf Kristensen edited this pageJun 22, 2025 ·17 revisions

Introduced with NLog 4.5

It is really easy:

  • Create a class that inherits fromNLog.Targets.TargetWithContext
  • Override theWrite(LogEventInfo logEvent) method.
  • In the body of this method invokethis.RenderLogEvent(this.Layout, logEvent) to get the message text, and invokethis.GetAllProperties(logEvent) to getstructured properties.

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

See also the updatedHow to write a custom async target

Example

usingNLog;usingNLog.Config;usingNLog.Targets;namespaceMyNamespace{[Target("MyFirst")]publicsealedclassMyFirstTarget:TargetWithContext{publicMyFirstTarget(){this.IncludeEventProperties=true;// Include LogEvent Properties by default}publicLayoutHost{get;set;}="localhost";protectedoverridevoidWrite(LogEventInfologEvent){stringlogMessage=this.RenderLogEvent(this.Layout,logEvent);stringhostName=this.RenderLogEvent(this.Host,logEvent);IDictionary<string,object>logProperties=this.GetAllProperties(logEvent);SendTheMessageToRemoteHost(hostName,logMessage,logProperties);}privatevoidSendTheMessageToRemoteHost(stringhostName,stringmessage,IDictionary<string,object>properties){// TODO - write me}}}

TargetWithContext Features

Additional Context Properties

Users can easily configure additional context information:

<targettype="MyFirst"name="first">   <contextpropertyname="MachineName"layout="${machinename}" />   <contextpropertyname="ThreadId"layout="${threadid}" /></target>

Without needing to inject the details upfront when doing the logging. It is automatically captured by the NLog engine.

Include ScopeContext

ScopeContext Properties can be used to provide context-specific details (Ex. request-correlationid fromILogger.BeginScope).

To automatically capture properties injected into ScopeContext (Before NLog 5.0 it was calledIncludeMDLC)

<targettype="MyFirst"name="first"includeScopeProperties="true">   ...</target>

Custom Merge Properties

TargetWithContext will by default ensure that all captured property-values are included in output, and will automatically generate new unique key-name when name-collission. If wanting to merge properties, so one context can override another context, then one can do it like this:

<targettype="MyFirst"excludeProperties="RequestId">     <contextpropertyname="RequestId"layout="${event-properties:RequestId:whenEmpty=${scope-property=RequestId:whenEmpty=DefaultValue}}" /></sometarget>

Instead ofDefaultValue then you could also use one of theNLog LayoutRenderers available. Ex.${aspnet-TraceIdentifier} or${activity:property=TraceId}. Or alternative skipwhenEmpty=DefaultValue and rely onIncludeEmptyValue="false" (default).

-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