.NET Core 2.2 中 SQL 服务器的 NLog 条目
NLog entries to SQL Server in .NET Core 2.2
我想做的是使用 NLog 将我所有的日志保存到一个单独的日志数据库中的 table,但我似乎无法让它工作。
这是我的 nlog 配置文件:
<?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"
autoReload="true"
internalLogLevel="Trace"
internalLogFile="c:\temp\internal-nlog.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<target name="dbLogger" xsi:type="Database"
connectionStringName="LoggingConnection" commandType="StoredProcedure"
commandText="[dbo].[NLog_AddEntry_p]">
<parameter name="@machineName" layout="${machinename}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@message" layout="${message}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@properties" layout="${all-event-properties:separator=|}" />
<parameter name="@callsite" layout="${callsite}" />
<parameter name="@exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="dbLogger"/>
</rules>
</nlog>
我设法让它登录到具有类似配置的 .txt 文件,但它没有写入数据库。 "LoggingConnection" 连接字符串与我在网站中用于正常 CRUD 操作的连接字符串相同;区别在于它使用了另一个数据库。
因此,在深入检查内部日志文件后,我想出了这个解决方案。
System.Data.SqlClient 已添加到项目中,这是必须的。此外,我还在项目中添加了 Nlog.Config Nuget 包。
我删除了 "extension" 标签并对 nlog 标签做了一些调整
<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="Trace" internalLogFile="c:\temp\nlog-internal.log">
我已将 "connectionStringName" 属性替换为 "connectionString" 并添加了 appSettings.json 中的连接字符串。
我想做的是使用 NLog 将我所有的日志保存到一个单独的日志数据库中的 table,但我似乎无法让它工作。
这是我的 nlog 配置文件:
<?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"
autoReload="true"
internalLogLevel="Trace"
internalLogFile="c:\temp\internal-nlog.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<target name="dbLogger" xsi:type="Database"
connectionStringName="LoggingConnection" commandType="StoredProcedure"
commandText="[dbo].[NLog_AddEntry_p]">
<parameter name="@machineName" layout="${machinename}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@message" layout="${message}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@properties" layout="${all-event-properties:separator=|}" />
<parameter name="@callsite" layout="${callsite}" />
<parameter name="@exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="dbLogger"/>
</rules>
</nlog>
我设法让它登录到具有类似配置的 .txt 文件,但它没有写入数据库。 "LoggingConnection" 连接字符串与我在网站中用于正常 CRUD 操作的连接字符串相同;区别在于它使用了另一个数据库。
因此,在深入检查内部日志文件后,我想出了这个解决方案。
System.Data.SqlClient 已添加到项目中,这是必须的。此外,我还在项目中添加了 Nlog.Config Nuget 包。
我删除了 "extension" 标签并对 nlog 标签做了一些调整
<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="Trace" internalLogFile="c:\temp\nlog-internal.log">
我已将 "connectionStringName" 属性替换为 "connectionString" 并添加了 appSettings.json 中的连接字符串。