我正在开发一个使用 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>
最佳答案
我认为你的项目中有一些奇怪的东西。我做了一个小仓库并得到了以下结果:
${identity}
确实显示notauth::
,这是正确的,因为我没有对某些内容进行身份验证 - 我们将其用于带有表单或窗口的 IIS身份验证。${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>
关于c# - NLog ${identity} 和 ${windows-identity} 保存为 "notauth",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52445927/