NLog ${identity} 和 ${windows-identity} 保存为 "notauth"

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

我正在开发一个使用 NLog 的 C# class 库项目。 NLog 正在记录到数据库。我已经设置了一个控制台应用程序测试项目来调用库(控制台应用程序没有日志记录)。我在日志 table 中有一列用于存储登录用户 运行 应用程序的用户名。

根据 NLog 的文档,该值存储在 ${identity} or ${windows-identity} 布局渲染器中。我试过同时使用两者,但是当日志写入数据库时​​,UserName 列的值为 notauth::。我怎样才能解决这个问题?我的 nlog.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"
      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>

我觉得你的项目有些奇怪。我做了一个小回购并得到以下结果:

  • ${identity} 确实显示 notauth::,这是正确的,因为我没有对某些东西进行身份验证 - 我们将其用于带表单的 IIS 或 windows 身份验证。

  • ${windows-identity} 显示 COFFEE-LAKE\Julian - 这是正确的。 "COFFEE-LAKE" 是我的机器名,Julian 也是正确的 :) 注意 - ${windows-identity} 永远不会渲染 notauth:: - 我检查了 NLog 的来源

在此处查看回购:https://github.com/304NotModified/nlog-console-notauth

当 运行 时,您的 "bin/Debug" 文件夹中有一个 "test1.log"。

注意:它具有以下配置:

<?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>