NLog ${identity} and ${windows-identity} saved as "notauth"

Viewed 1620

I'm working on a C# class library project that is using NLog. NLog is logging to a database. I've set up a console app test project to call the library (the console app has no logging). I have a column in the log table to store the user name of the logged in user running the application.

According to NLog's documentation, that value is stored in either the ${identity} or ${windows-identity} layout renderers. I've tried using both, but when the log writes to the database, the value of the UserName column is notauth::. How can I fix this? My nlog.config file is below.

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" 
      internalLogFile="c:\temp\nlog-internal.log"
      throwConfigExceptions="true">
  <targets>
    <target name="db"
            xsi:type="Database"
            connectionString="Server=ServerName;Database=DatabaseName;Integrated Security=True;">
      <commandText>
        INSERT INTO Logs.TableName
        (
            [Level]
          , UserName
          , CallSite
          , [Message]
          , Exception
          , StackTrace
          , Logged
        )
        VALUES
        (
            @level
          , @userName
          , @callSite
          , @message
          , @exception
          , @stackTrace
          , @logged
        )
      </commandText>
      <parameter name="@level" layout="${level}" />
      <parameter name="@userName" layout="${identity}" />
      <parameter name="@callSite" layout="${callsite}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@exception" layout="${exception:tostring}" />
      <parameter name="@stackTrace" layout="${stacktrace}" />
      <parameter name="@logged" layout="${date}" />
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="db" />
  </rules>
</nlog>
1 Answers

I think there is something strange in you project. I made a small repo and have the following results:

  • ${identity} shows indeed notauth::, that's correct as I don't authenticate to something - we use this for IIS with forms or windows authentication.

  • ${windows-identity} shows COFFEE-LAKE\Julian - which is correct. "COFFEE-LAKE" is my machine name and Julian is also correct :) note - ${windows-identity} will never render notauth:: - I checked NLog's source

See repo here: https://github.com/304NotModified/nlog-console-notauth

When running, there is a "test1.log" in your "bin/Debug" folder.

note: it has the following config:

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
  <targets>
    <target xsi:type="File" name="f"
             fileName="${basedir}/test1.log"
            layout="${longdate}|${level:uppercase=true}|${logger}|${message}|identity: '${identity}' | windows-identity: '${windows-identity}'" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="f" />
  </rules>
</nlog>
Related