【发布时间】:2020-04-15 13:55:17
【问题描述】:
我对 NLog 有一些问题。据here说的
如果您想要自定义布局属性(NLog 将它们称为布局渲染器),您可以使用 EventProperties 布局渲染器。
我写了一些配置:
<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="on" internalLogFile="c:\temp\nlog-internal.log">
<targets>
<!-- database target -->
<target name="databaseauthentication"
xsi:type="Database"
connectionString="Data Source = [....]; Initial Catalog = [....]; User Id = [....]; Password = [....]"
commandText="exec dbo.InsertAuthentication
@company,
@firstname,
@lastname,
@ip,
@pcname,
@additionalInfo">
<parameter name="@company" layout="${event-properties:item=companyValue}" />
<parameter name="@firstname" layout="${event-properties:item=firstnameValue}" />
<parameter name="@lastname" layout="${event-properties:item=lastnameValue}" />
<parameter name="@ip" layout="${event-properties:item=ipValue}" />
<parameter name="@pcname" layout="${event-properties:item=pcnameValue}" />
<parameter name="@additionalInfo" layout="${event-properties:item=additionalInfoValue}" />
</target>
</targets>
<rules>
<logger levels="Info" name="asyncdatabaseauthenticationLogger" writeTo="asyncdatabaseauthentication"/>
<logger levels="Info" name="databaseauthenticationLogger" writeTo="databaseauthentication"/>
</rules>
</nlog>
并像这样使用:
public static void SendLogin()
{
var eventInfo = new LogEventInfo(LogLevel.Info, databaseAuthenticateLogger.Name, "Message");
eventInfo.Properties["firstnameValue"] = "My Fist Name;
eventInfo.Properties["lastnameValue"] = "My Last Name";
eventInfo.Properties["companyValue"] = "My Company";
eventInfo.Properties["ipValue"] = "IP";
eventInfo.Properties["pcnameValue"] = "PC Name";
eventInfo.Properties["additionalInfoValue"] = "Login";
databaseAuthenticateLogger.Log(eventInfo);
}
但是这段代码不起作用。谁能告诉我我的错误在哪里?
【问题讨论】:
-
很抱歉,这个问题令人困惑,对其他人没有多大帮助。这与数据库或自定义属性无关。请阅读
<rules>的工作原理。见github.com/nlog/nlog/wiki/Configuration-file#logger-name-filter