Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET Core 2.2中SQL Server的NLog条目_C#_Logging_Asp.net Core_Nlog - Fatal编程技术网

C# .NET Core 2.2中SQL Server的NLog条目

C# .NET Core 2.2中SQL Server的NLog条目,c#,logging,asp.net-core,nlog,C#,Logging,Asp.net Core,Nlog,我想做的是使用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

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

因此,在深入检查了内部日志文件之后,我想出了这个解决方案

System.Data.SqlClient已添加到项目中,需要对其进行修改。此外,我还向项目中添加了Nlog.Config numget包

我删除了“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">


我已将“connectionString”属性替换为“connectionString”,并从appSettings.json中添加了连接字符串。

您检查过了吗?您添加了nuget软件包吗?请记住,
connectionStringName
仅适用于NetFramework。另见。在NLog.Web.AspNetCore版本中将
connectionString
${configsetting}
一起使用。4.8.0您可以使用
${configsetting}
直接从appsetting.json读取值。另请参阅(如果从NLog.Web.AspNetCore或NLog.Extension.Hosting调用
UseNLog()
,将自动注册)