0

i'm developing a simple .NET application and i'm tryng to use log4Net to log some usefull info. With the basic configuration i have no problem. When i try to use an xml configuration i have the following problem:

log4net:ERROR Error while loading XML configuration System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at log4net.Config.XmlConfigurator.InternalConfigure(ILoggerRepository repository, Stream configStream)

The piece of code is the following:

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

The xml file is taked directly from log4net documentation and is the following

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

how can i solve that error, i think the xml is correct assuming there is no error in documentation.

Razlo3p
  • 455
  • 2
  • 7
  • 28
  • 2
    Check your XML for nonprintable characters, http://stackoverflow.com/questions/291455/xml-data-at-root-level-is-invalid – CodeCaster Oct 05 '15 at 10:34
  • I just figured out myself. Wrong parentesis on Get directory name, i will post an autoanswer Asap! – Razlo3p Oct 05 '15 at 10:39

1 Answers1

0

Solved, i just typed wrong a parentesis. I will answer this for community

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

Razlo3p
  • 455
  • 2
  • 7
  • 28
  • 4
    You might want to take a look at [`Path.Combine`](https://msdn.microsoft.com/en-us/library/system.io.path.combine(v=vs.110).aspx) also. With only 1 `Path.DirectorySeparatorChar` it won't make _too_ much difference, but as soon as you start having a few path components to concatenate it makes a difference. It also takes care of whether each component has a `/` at the start/end or not etc. – James Thorpe Oct 05 '15 at 10:51