15

I want to add a string to each row on my NLog output. The logic stays the same, trying to get the current user and if succeed, add the current user to the output.

I know how to implement it each time, but I want to set this template at one place and not repeat it on each writing.

Julian
  • 33,915
  • 22
  • 119
  • 174
Shahar
  • 541
  • 2
  • 7
  • 22

4 Answers4

22

The WindowsIdentityLayoutRenderer should probably give you what you want. You can choose to log either the Domain, the UserName, or both.

You would configure it something like this (untested) to your NLog.config file:

<targets>
    <target name="file" xsi:type="File" 
        layout="${longdate} | ${level} | ${logger} | ${windows-identity} | ${message}"
        fileName="${basedir}/${shortdate}.log" />
</targets>

This might not work in a low privilege environment.

How do you get the user name now? If you get it something like this:

HttpContext.Current.User.Identity.Name

Then you can use NLog's "aspnet-user-identity" LayoutRenderer, something like this:

<targets>
    <target name="file" xsi:type="File" 
        layout="${longdate} | ${level} | ${logger} | ${aspnet-user-identity} | ${message}"
        fileName="${basedir}/${shortdate}.log" />
</targets>

NLog's aspnet* LayoutRenderers are in NLog.Extended.sll, so you will need that dll in addition to NLog.dll.

Julian
  • 33,915
  • 22
  • 119
  • 174
wageoghe
  • 27,390
  • 13
  • 88
  • 116
  • 1
    My authentication is based on form authentication. any suggestion? – Shahar Nov 19 '13 at 21:12
  • Is that ASP.NET? I have not done any development using that, but NLog has several LayoutRenderers that can be used to retrieve ASP.NET parameters, including UserIdentity. – wageoghe Nov 19 '13 at 21:37
  • 13
    To others who come after, this is now in NLog.Web, not NLog.Extended – Daniel Crenna Jul 10 '15 at 17:15
  • what if `${aspnet-user-identity}` is empty ?! Can I assign `HttpContext.Current.User.Identity.Name` directly ? – Muflix Jul 09 '18 at 14:04
  • The code behind `${aspnet-user-identity}` just retrieves `HttpContext.Current.User.Identity.Name`, so I would not expect getting the name directly to yield anything different. – wageoghe Jul 09 '18 at 22:22
  • adding ${windows-identity} worked like charm for the windows identity – Ak777 Apr 14 '20 at 20:54
  • For some reason, this still doesnt work, using .netcore 2.2 web api, enabled windows authentication to the web api but it logs empty value. is this because, i also have the anonymous auth enabled? – Ak777 Jul 15 '20 at 06:43
2

For those using custom authentication/authorization (or can't use the windows identity for some reason) and are using .NET core (3.*) you can use the MappedDiagnosticsContext to inject custom key-values.

As soon as you know the identity of the user you can set it like so:

using NLog;
//...
MappedDiagnosticsContext.Set("UserName", "Some user name");

Always make sure you do this in a Scoped manner (using .NET core Dependency Injection terms, or a Per Request Scope), since you don't want to mix up users when logging.

In your NLog config you can then read the value by using this syntax ${mdc:UserName}.

I'm using the following nuget packages:

  • nlog
  • nlog.web.aspnetcore
Aage
  • 5,932
  • 2
  • 32
  • 57
1

NLog ver. 4.6.4 introduced ${environment-user} that works on both Windows and Linux. It resolves to System.Environment.UserName

See also: https://github.com/NLog/NLog/wiki/Environment-User-Layout-Renderer

For ASP.NET application there is also ${aspnet-user-identity} for the user-identity currently logged on.

See also: https://github.com/NLog/NLog/wiki/AspNetUserIdentity-layout-renderer

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
1

I didn't use Microsoft identity, so for adding username I just added it to the context in a middleware:

public class MyMiddleware
{
    private readonly RequestDelegate _next;

    public MyMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        // ... Get UserName and store it to MyUserName

        context.Items.Add("UserName", MyUserName);

        await _next(context);
    }
}

then in NLog config I used ${aspnet-item:variable=UserName} to render user name from context.

Mojtaba
  • 205
  • 3
  • 10