首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NLog ${identity}和${windows-identity}保存为"notauth“

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

Stack Overflow用户
提问于 2018-09-21 14:31:27
回答 1查看 1.7K关注 0票数 2

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

根据NLog的文档,该值存储在${identity}${windows-identity}布局呈现器中。我尝试使用这两种方法,但是当日志写入数据库时,UserName列的值是notauth::。我怎么才能解决这个问题?我的nlog.config文件在下面。

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-26 16:01:50

我觉得你的计划有些奇怪。我做了一个小回购,结果如下:

  • ${identity}确实显示了notauth::,这是正确的,因为我不对某些东西进行身份验证--我们将它用于IIS和窗体或windows身份验证。
  • ${windows-identity}显示了COFFEE-LAKE\Julian --这是正确的。“咖啡湖”是我的机器名,朱利安也是正确的:) 注意事项- ${windows-identity}永远不会呈现notauth:: -我检查了NLog的源代码

见这里的回购:https://github.com/304NotModified/nlog-console-notauth

运行时,"bin/Debug“文件夹中有一个"test1.log”。

注意:它有以下配置:

代码语言:javascript
复制
<?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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52445927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档