NLog extensions for displaying User Windows Identity and target wrapper for user impersonation
-
Install the package
Install-Package NLog.WindowsIdentity
or in your csproj:<PackageReference Include="NLog.WindowsIdentity" Version="5.*" />
-
Add to your nlog.config:
<extensions> <add assembly="NLog.WindowsIdentity"/> </extensions>
Alternative register from code using fluent configuration API:
LogManager.Setup().SetupExtensions(ext => { ext.RegisterTarget<NLog.Targets.Wrappers.ImpersonatingTargetWrapper>(); ext.RegisterLayoutRenderer<NLog.LayoutRenderers.WindowsIdentityLayoutRenderer>(); });
Example of NLog.config
-file that outputs username from Windows Identity :
<nlog>
<extensions>
<add assembly="NLog.WindowsIdentity"/>
</extensions>
<targets>
<target name="console" xsi:type="console" layout="${message}|User=${windows-identity}" />
</targets>
<rules>
<logger minLevel="Info" writeTo="console" />
</rules>
</nlog>
Example of NLog.config
-file that apply ImpersonatingWrapper :
<nlog>
<extensions>
<add assembly="NLog.WindowsIdentity"/>
</extensions>
<targets>
<target name="userConsole" xsi:type="ImpersonatingWrapper" userName="xxx">
<target name="console" xsi:type="console" layout="${message}|User=${windows-identity}" />
</target>
</targets>
<rules>
<logger minLevel="Info" writeTo="userConsole" />
</rules>
</nlog>