2016-08-15 76 views
2

我一直試圖使用NLog.Mongo和NLog.MongoDB將日誌寫入Mongo數據庫。但是,我不斷收到一條錯誤消息:「這是一個無效的xsi:type'http://www.nlog-project.org/schemas/NLog.xsd:MongoDB」。我使用的代碼如下:使用NLog將日誌存儲在Mongo數據庫中

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

回答

2

,來了只指定它不是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> 
+0

注意'的databaseName = 「NLOG」','集合名= 「測試」'和'/ NLOG /'的connectionString的都是可選的。如果排除默認值將被使用。 (NLog將用作數據庫名稱,日誌將用作集合名稱)。必要的部分只是'connectionString =「mongodb:// localhost」' – Roars

+0

請將此標記爲anwer – Julian

+0

會這樣做。 (說明天之前我不能這樣做) – Roars

相關問題