使用NLog将日志存储在Mongo数据库中

时间:2016-08-15 10:14:15

标签: xml mongodb nlog

我一直在尝试使用NLog.Mongo和NLog.MongoDB将日志写入Mongo数据库。但是,我不断收到错误消息“这是一个无效的xsi:type'http://www.nlog-project.org/schemas/NLog.xsd:MongoDB”。我正在使用的代码如下:

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

我要么寻找这个错误的解决方案,要么可以执行其他方式。我已经搜遍了我能想到的任何地方,只发现了未解决的问题,例如:

NLog xsi:type not working with custom target

Nlog with MongoDB connection and target

1 个答案:

答案 0 :(得分:3)

出现的错误仅表明它不是NLog的识别类型。扩展确实有效,事实证明上面代码的问题是由于没有指定connectionString。

我已使用以下方法解决了问题:

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