Nlog: Aspnet-User-Identity layout renderer throws on asp.net core 2.1

Created on 12 Jun 2018  路  2Comments  路  Source: NLog/NLog

NLog version: NLog.Web.AspNetCore 4.5.4

Platform: ASP.NET Core 2.1

Current NLog config (xml or C#, if relevant)

<?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"
      autoReload="true">

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <targets>
    <target xsi:type="File"
            name="allfile"
            layout="${longdate}|${event-properties:item=EventId_Id}|${logger}|${aspnet-mvc-controller}|${aspnet-mvc-action}|${aspnet-request-method}|${aspnet-user-indentity}|${uppercase:${level}}|${message} ${exception:format=tostring}"
            fileName="logs\logfile-all.log"
            archiveFileName="logs\logfile-all.{#}.log"
            archiveEvery="Day"
            archiveNumbering="DateAndSequence"
            archiveDateFormat="yyyy-MM-dd"
            maxArchiveFiles="31"
            keepFileOpen="true" />
    <target xsi:type="File"
            name="file"
            layout="${longdate}|${event-properties:item=EventId_Id}|${logger}|${aspnet-mvc-controller}|${aspnet-mvc-action}|${aspnet-request-method}|${aspnet-user-indentity}|${uppercase:${level}}|${message} ${exception:format=tostring}"
            fileName="logs\logfile.log"
            archiveFileName="logs\logfile.{#}.log"
            archiveEvery="Day"
            archiveNumbering="DateAndSequence"
            archiveDateFormat="yyyy-MM-dd"
            maxArchiveFiles="31"
            keepFileOpen="true" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="allfile" />
    <logger name="Microsoft.*" maxLevel="Info" final="true" />
    <logger name="*" minlevel="Trace" writeTo="file" />
  </rules>
</nlog>
public class Program
{
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureLogging((context, logging) =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(LogLevel.Trace);

                if (context.HostingEnvironment.IsDevelopment())
                {
                    logging.AddDebug();
                }
            })
            .UseNLog();
    // ...
}
public class Startup
{
    // ...
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        // ...
    }
}
  • What is the current result?
    The layout renderer does not populate the value.
  • What is the expected result?
    The value is populated.
  • Did you checked the Internal log?
    Yes
  • Please post full exception details (message, stacktrace, inner exceptions)
2018-06-12 12:57:06.1501 Error Error parsing layout aspnet-user-indentity will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-user-indentity'. Is NLog.Web not included?
   at NLog.Config.Factory`2.CreateInstance(String name)
   at NLog.Layouts.LayoutParser.GetLayoutRenderer(ConfigurationItemFactory configurationItemFactory, String name)
  • Are there any workarounds?
    None that I am aware. The NLog.Web package lists the NLog.Web.AspNetCore as the correct package to use. The dependencies on nuget.org list NLog.Extensions.Logging as required, however that package states its not applicable.
  • Is there a version in which it did work?
    I have not tried other versions. What is the correct set of dependencies, and is this is an asp.net core 2.1 issue or am I missing a package that was not automatically resolved by the VS package manager?

Most helpful comment

Words cannot convey the level of personal failure and shame:) It's now logging the account name...

All 2 comments

@jcasale You have a small spelling error: ${aspnet-user-indentity} should be ${aspnet-user-identity}

Words cannot convey the level of personal failure and shame:) It's now logging the account name...

Was this page helpful?
0 / 5 - 0 ratings