3
votes

I have been trying to use NLog.Mongo and NLog.MongoDB to write logs to a Mongo database. However, I keep getting an error saying "This is an invalid xsi:type 'http://www.nlog-project.org/schemas/NLog.xsd:MongoDB". The code I am using is the following:

<?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"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  throwExceptions="false"
  internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <extensions>
    <add assembly="NLog.MongoDB"/>
  </extensions>

  <targets>
    <target xsi:type="MongoDB" name="mongo" database="NLog">
      <field name="timestamp" layout="${date}"/>
      <field name="level" layout="${level}"/>
      <field name="message" layout="${message}"/>
    </target>
  </targets>

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

I am either looking for a solution to this error or another way this could be performed. I have searched everywhere I can think of and only found unanswered questions such as:

NLog xsi:type not working with custom target

Nlog with MongoDB connection and target

1

1 Answers

3
votes

The error that comes up is only specifying that it isn't a recognised type of NLog. The extension does actually work, it turns out the problem with the code above is due to not specifying the connectionString.

I have fixed the problem using:

<?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"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  throwExceptions="false"
  internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <extensions>
    <add assembly="NLog.Mongo"/>
  </extensions>

  <targets>
    <target xsi:type="Mongo" name="mongo" databaseName="NLog" collectionName="Test" connectionString="mongodb://localhost/NLog"/>
  </targets>

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