Bilal Fazlani
Bilal Fazlani

Reputation: 6967

passing values to nlog custom target

I have created a custom nLog target and it is working except for one thing. I want to pass the logged in person name to the target, I have created a property called ApplicationUser which will pick up value.

The target definition looks like this :

<target name="MemoryTrace" 
        xsi:type="CustomTraceListener" 
        ApplicationUser="${identity:authType=False:isAuthenticated=False}"  />

But when the customTarget recieves the value of ApplicationUser, it does not get resolved to logged in person's name it just stays as ${identity:authType=False:isAuthenticated=False}.

This is all as per the nLog documentation

I have tested this with other targets and it gets resolved. What is it that I have to do to resolve it to username ?

Upvotes: 2

Views: 2512

Answers (1)

nemesv
nemesv

Reputation: 139748

You need to define your ApplicationUser with the type Layout then you can get the current user with "rendering the layout" using the ApplicationUser.Render(logevent):

[Target("CustomTraceListener")]
public sealed class MyFirstTarget : TargetWithLayout
{

    public Layout ApplicationUser { get; set; }

    protected override void Write(LogEventInfo logEvent)
    {
        string logMessage = this.Layout.Render(logEvent);
        string applicationUser = ApplicationUser.Render(logEvent);
        // ... Write where you want
    }
} 

Upvotes: 4

Related Questions