2015-10-05 109 views
0

我正在開發一個簡單的.NET應用程序,我tryng使用log4net的登錄一些有用的信息。 基本配置我沒有問題。 當我嘗試使用xml配置時,出現以下問題:log4net的無法解析的XML配置

log4net:錯誤加載XML配置時出錯System.Xml.XmlException:根級別的數據無效。 1號線,在System.Xml.XmlTextReaderImpl.Throw(例外五)在System.Xml.XmlTextReaderImpl.Throw(字符串RES,字符串ARG)在System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()在System.Xml.XmlTextReaderImpl位置1。 ParseDocumentContent()在System.Xml.XmlTextReaderImpl.Read()在System.Xml.XmlLoader.Load(XmlDocument的文檔,讀取器的XmlReader,布爾preserveWhitespace)在System.Xml.XmlDocument.Load(的XmlReader讀取器)在log4net.Config.XmlConfigurator。 InternalConfigure(ILoggerRepository庫,流configStream)

的一段代碼如下:

var xmlConfig = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location+path.DirectorySeparatorChar+"logConfig.xml"); 
if (xmlConfig != null) XmlConfigurator.Configure(new FileInfo(xmlConfig)); 
else BasicConfigurator.Configure(); 

XML文件是直接從log4net的文檔抽放和如下

<log4net> 
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > 
     <layout type="log4net.Layout.PatternLayout"> 
      <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> 
     </layout> 
    </appender> 
    <root> 
     <level value="INFO" /> 
     <appender-ref ref="ConsoleAppender" /> 
    </root> 
</log4net> 

我該如何解決這個錯誤,我認爲XML是正確的假設有在文檔中沒有錯誤。

+2

檢查XML的非打印字符,http://stackoverflow.com/questions/291455/xml-data-at-root-level-is-invalid – CodeCaster

+0

我只是想出我自己。獲取目錄名稱時出現錯誤的parentesis,我將發佈一個autoanswer Asap! – Razlo3p

回答

0

解決,我剛纔輸入錯了一個parentesis。我會回答這個社區

var xmlConfig = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "logConfig.xml";

+3

你可能想看看['Path.Combine'(https://msdn.microsoft.com/en-us/library/system.io.path.combine(V = vs.110)的.aspx)也。由於只有1'Path.DirectorySeparatorChar'它不會使_too_太大的差別,但只要你開始有幾個路徑組件來連接它的確與衆不同。它還會考慮每個組件在開始/結束時是否有'/'等。 –