sfinks_29
sfinks_29

Reputation: 918

NLog exception when writing to a database

I have NLog 4.2.3 configured to write to a database and a text file using the following config:

<?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>

I'm trying to log a simple Info message. It logs correctly to the text file, but it fails for the database and I get the following error in the NLog internal log file:

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)

I have looked at configs from other similar questions (NLog is not writing to database table, NLog to PostgreSQL connection), but I can't see what is missing from mine?

Upvotes: 1

Views: 815

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40838

According to the nlog docs, dbProvider is an invariant name for a factory registered in a config file OR the fully qualified name of a type that implements IDbConnection. Npgsql.NpgsqlConnection implements the required interface and has a parameterless contructor. NpgsqlFactory does not and it does not have a parameterless constructor (the cause of the error).

Upvotes: 1

Related Questions