NLog Target for writing logevents to Event Tracing for Windows (ETW).
dotnet add package NLog.Etw
Example of NLog.config
-file that writes to ETW:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"">
<extensions>
<add assembly="NLog.Etw" />
</extensions>
<targets async="true">
<target type="EtwEventSource"
name="eetw"
providerName="MyEventSourceName"
taskName="${level}"
layout="${message}"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="eetw" />
</rules>
</nlog>
Example of providing own custom EventSource for writing events:
[EventSource(Name = "MyEventSourceName")]
public class MyEventSource : EventSource, NLog.Etw.INLogEventSource
{
/// <inheritdoc/>
[NonEvent]
public void Write(EventLevel eventLevel, string layoutMessage, LogEventInfo logEvent)
{
// TODO Call own custom logging-method
}
/// <inheritdoc/>
EventSource EventSource { get { return this; } }
internal readonly static MyEventSource Log = new MyEventSource();
}
var config = new NLog.Config.LoggingConfiguration();
config.AddRuleForAllLevels(new NLog.Etw.NLogEtwExtendedTarget(MyEventSource.Log) { Name = "eetw" });
NLog.LogManager.Configuration = config;
Example of NLog.config
-file that writes to ETW-EventCounter:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Etw" />
</extensions>
<targets async="true">
<target type="EtwEventCounter"
name="etwCounter"
providerName="MyEventSourceName"
counterName="MyEventCounterName"
metricValue="${event-properties:MyMetricValue:whenEmpty=1}"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="etwCounter" />
</rules>
</nlog>