【问题标题】:NLog entries to SQL Server in .NET Core 2.2NLog 条目到 .NET Core 2.2 中的 SQL Server
【发布时间】:2019-01-27 14:35:26
【问题描述】:

我想要做的是使用 NLog 将我的所有日​​志保存到单独的日志数据库中的一个表中,但我似乎无法让它工作。

这是我的 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 操作的连接字符串相同;不同之处在于它使用了另一个数据库。

【问题讨论】:

标签: c# logging asp.net-core nlog


【解决方案1】:

所以,经过一番挖掘检查内部日志文件后,我想出了这个解决方案。

System.Data.SqlClient 已添加到项目中,并且需要添加。此外,我还在项目中添加了 Nlog.Config Nuget 包。

我已经删除了“扩展”标签,并对 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 的连接字符串。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 2020-08-22
相关资源
最近更新 更多