写入数据库时​​出现NLog异常

时间:2016-01-21 01:03:55

标签: c# .net postgresql nlog

我已将NLog 4.2.3配置为使用以下配置写入数据库和文本文件:

<?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="Error" internalLogFile="C:/tmp/NLog.txt">
    <targets>
      <target name="database" xsi:type="Database" dbProvider="Npgsql.NpgsqlFactory,Npgsql,Version=2.0.12.00,Culture=neutral,PublicKeyToken=5d8b90d52f46fda7" connectionString="Server=xxx.xxx.xxx.xxx;Port=5432;User Id=userid;Password=password;Database=testdb;">
      <commandText>INSERT INTO invoice_log (message, level, logger) VALUES (@msg, @level, @logger)</commandText>

      <parameter name="@msg" layout="${message}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@logger" layout="${logger}" />
    </target>

    <target name="log" xsi:type="File" layout="${longdate} | ${logger} | ${level:uppercase=true}: ${message}" fileName="${basedir}/Logs.txt" keepFileOpen="false" encoding="iso-8859-2" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="database" />
    <logger name="*" minlevel="Trace" writeTo="log" />
  </rules>
</nlog>

我试图记录一条简单的Info消息。它正确记录到文本文件,但它对数据库失败,我在NLog内部日志文件中收到以下错误:

Error Error when writing to database System.MissingMethodException: No parameterless constructor defined for this object.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at NLog.Targets.DatabaseTarget.OpenConnection(String connectionString)
   at NLog.Targets.DatabaseTarget.EnsureConnectionOpen(String connectionString)
   at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent)
   at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)

我查看了其他类似问题(NLog is not writing to database tableNLog to PostgreSQL connection)的配置,但我无法看到我的遗失内容?

1 个答案:

答案 0 :(得分:1)

根据nlog docs,dbProvider是在配置文件中注册的工厂的不变名称,或者是实现IDbConnection的类型的完全限定名称。 Npgsql.NpgsqlConnection实现了所需的接口,并具有无参数的构造函数。 NpgsqlFactory没有,它没有无参数构造函数(错误原因)。