.net 4.8 控制台应用程序的 Nlog 配置写入数据库 - 未写入日志

问题描述 投票:0回答:1

我有一个 C#.Net 4.8 控制台应用程序,我尝试使用 NLog 记录到 MSSQL 数据库。

我的申请有以下内容:

private static Logger nLogger = LogManager.GetLogger("myLogger");

static void Main()
{
    nLogger.Info("ConsoleApp Started");
    // MAIN code of application
    nLogger.Info("ConsoleApp Ended");
    LogManager.Flush();
}

我的 app.config 文件具有以下内容:

<configuration>
<configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="ConsoleApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  autoReload="true">

    <!-- the targets to write to -->
    <targets async="true">
        <target name="db" xsi:type="Database"
                connectionString="server=127.0.0.1\test;Database=TicketDB_Test;user id=USER;password=PASSWORD"
                commandType="StoredProcedure"
                commandText="[dbo].[NLog_AddEntry]">
            <parameter name="@machineName" layout="${machinename}" />
            <parameter name="@level"       layout="${level}"/>
            <parameter name="@message"     layout="${message}"/>
            <parameter name="@logger"      layout="${processname}" />
            <parameter name="@loggertype"  layout="${literal:text=ConsoleApplication}" />
            <parameter name="@properties"  layout="${all-event-properties:separator=|}" />
            <parameter name="@callsite"    layout="${callsite}"/>
            <parameter name="@linenumber"  layout="${callsite-linenumber}" />
            <parameter name="@exception"   layout="${exception:tostring}"/>
            <parameter name="@stackTrace"  layout="${stacktrace}"/>
        </target>
    </targets>
    <!-- rules to map from logger name to target -->
    <rules>
        <logger name="myLogger" minlevel="Trace" writeTo="db" />
    </rules>
</nlog>
<applicationSettings>
    <ConsoleApp.Properties.Settings>
        //...
    </ConsoleApp.Properties.Settings>
</applicationSettings>
</configuration>

该解决方案具有版本 5.2.4 的 NLog 和 NLog.Database 包

当控制台应用程序运行时,不会将任何内容记录到数据库中。存储过程 NLog_AddEntry 已检查并且有效。

我已经想不出为什么 NLog 没有记录到数据库了?

c# console-application app-config nlog
1个回答
0
投票

@RolfKristensen 感谢您的建议。内部记录器给了我最终的答案…我在目标中输错了一个参数名称…很有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.