2

I'm using log4net for the first time, and I can't figure out how to add the appropriate config settings. All the documentation is pretty consistent about adding a <log4net> section to the app.config file, but for it to compile correctly, don't I also need to outline my configSections?

I have the following right now:

<configuration>
  <configSections>
    <section name="system.serviceModel"/>
    <section name="appSettings" type="System.Configuration.ConfigurationManager"/>
    <section name="log4net"
       type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <section name="startup" />
  </configSections>
  <system.serviceModel>
   ...
  </system.serviceModel>
  <appSettings>
   ...
  </appSettings>
  <log4net>
  ...
  </log4net>
  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>

But I'm receiving the following errors:

  • XML document must contain a root level element
  • The required attribute 'type' is missing (from system.serviceModel and startup)
  • Could not find schema information for the element * (*=everything in log4net)

I've read a couple posts on section groups, and I've considered setting up the appSettings and log4net in a separate config file. This is a little over my head.

Should I be using separate config files?

If I should be putting everything in one config file, how do I know what type a section is? (I'm guessing on the appSettings type based on the assembly I use to get the settings--and I got the type for log4net from the many posts including it.)

tmoore82
  • 1,857
  • 1
  • 27
  • 48
  • 1
    Why do you redeclare the "system.serviceModel", "appSettings" and "startup"?. They are already correctly declared at higher level (machine.config?), usually you don't need to redeclare them here. Also do you have at the beginning of the config file? – Steve Aug 15 '13 at 20:32
  • 1
    I agree with Steve, the only config section that you need to declare is log4net. Remove all the other ones, and make sure the `` is at the very top. If for some odd reason you have to declare them, then you have to specify their `type` attribute. – gitsitgo Aug 15 '13 at 20:39

1 Answers1

1

Remove the duplicate declaration in the configSections for "appSettings", "system.serviceModel", "startup".
They are already declared in the file machine.config installed by the Framework in the appropriate subfolder of C:\WINDOWS\Microsoft.Net

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4Net" />
  </configSections>
  <appSettings>
     ....
  </appSettings>
  <log4net>
    <root>
       .....
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
       .....
    </appender>
  </log4net>
  <system.serviceModel>
      ....
  </system.serviceModel>
</configuration>

also be sure that your config file starts with <?xml version="1.0"?>

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Tried to mimic this as closely as possible. I did have the xml header, though I missed it in the copy. Still getting the error that the `XML document must contain a root level element` and all the messages about elements not being found. Also noticed that your response doesn't include ``. Was that deliberate? – tmoore82 Aug 15 '13 at 20:47
  • No, just a copy/paste from a config of mine. No startup there. – Steve Aug 15 '13 at 20:49
  • 1
    I suggest to try to remove sections from the config and check when the error disappears. Probably you have something wrong somewhere in the file. By the way, do you have other projects with simple config working correctly? – Steve Aug 15 '13 at 20:52
  • Everything was working until I started trying to add the log4net stuff. So I commented out everything except what was there before (`system.serviceModel` and `startup`), and on `startup` I'm getting the `Could not find schema information` for everything there. – tmoore82 Aug 15 '13 at 20:54
  • If you don't have sensitive informations in your config file, I suggest to add all the config in your question. I really think that there is something wrong in the file, but as it stands now it is impossible to say what is wrong – Steve Aug 15 '13 at 21:03
  • Followed the suggestion here [link](http://stackoverflow.com/questions/4355278/visual-studio-always-selects-the-wrong-xsd-for-app-config/4400071#4400071), and the messages on the startup section went away. I really appreciate your help. I'm late for a meeting. I'll parse out sensitive info and post the full file later. But let me ask you this: should I have a `DotNetConfig40.xsd` file? I don't have one. – tmoore82 Aug 15 '13 at 21:15
  • If it makes a difference, I copied this assembly reference from a website. Maybe the problem is here? `[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]` – tmoore82 Aug 15 '13 at 21:25
  • I have no idea where _log4net.config_ is, or if that's something I'm supposed to create. I tried to make it refer to my app.config. No dice. – tmoore82 Aug 15 '13 at 21:26
  • A last idea. Did you save your config file using Visual Studio or did you modify with a different editor and saved it in UNICODE or UTF-8 instead of normal ANSI text? – Steve Aug 15 '13 at 21:37
  • 1
    Took the project home, loaded and built it in VS2012 (Windows 8), brought it back in today, and everything works. Not sure why. Marking your input as the answer for thoroughness. Thanks for the help! – tmoore82 Aug 16 '13 at 12:21