gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:32:15 25 4
gpt4 key购买 nike

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

根据 NLog 的文档,该值存储在 ${identity} 中。或 ${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>

最佳答案

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

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

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

在此处查看 repo :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>

关于c# - NLog ${identity} 和 ${windows-identity} 保存为 "notauth",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52445927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com