views:

6330

answers:

10

I decided to use log4net as a logger for a new webservice project. Everything is working fine, but I get a lot of messages like the one below, for every log4net tag I am using in my web.config:

Could not find schema information for the element 'log4net'...

Below are the relevant parts of my web.config:

  <configSections>
    <section name="log4net" 
        type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level: %message%newline" />
      </layout>
    </appender>
    <logger name="TIMServerLog">
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
    </logger>
  </log4net>

Solved:

  1. Copy every log4net specific tag to a separate xml-file. Make sure to use .xml as file extension.
  2. Add the following line to AssemblyInfo.cs:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "xmlFile.xml", Watch = true)]

nemo added:

Just a word of warning to anyone follow the advice of the answers in this thread. There is a possible security risk by having the log4net configuration in an xml off the root of the web service, as it will be accessible to anyone by default. Just be advised if your configuration contains sensitive data, you may want to put it else where.

+1  A: 

Have you tried using a separate log4net.config file?

wcm
+1  A: 

I believe you are seeing the message because Visual Studio doesn't know how to validate the log4net section of the config file. You should be able to fix this by copying the log4net XSD into C:\Program Files\Microsoft Visual Studio 8\XML\Schemas (or wherever your Visual Studio is installed). As an added bonus you should now get intellisense support for log4net

Wheelie
This didn't work for me.
James McMahon
A: 

@wcm: I tried using a separate file. I added the following line to AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

and put everything dealing with log4net in that file, but I still get the same messages.

xsl
The main reason I asked is that I have a separate log4net.config file and the only reference to log4net in my web.config file is: <add key="log4net.Internal.Debug" value="true"/>. If may be that Wheelie's suggestion will solve your problem. Sorry.
wcm
+4  A: 

I had a different take, and needed the following syntax:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.xml", Watch = true)]

which differs from xsl's last post, but made a difference for me. Check out this blog post, it helped me out.

steve_mtl
Thanks Steve, I have spent all day trying to fix this issue and this worked.
James McMahon
+2  A: 

@steve_mtl: Changing the file extensions from .config to .xml solved the problem. Thank you.

@Wheelie: I couldn't try your suggestion, because I needed a solution which works with an unmodified Visual Studio installation.


To sum it up, here is how to solve the problem:

  1. Copy every log4net specific tag to a separate xml-file. Make sure to use .xml as file extension.
  2. Add the following line to AssemblyInfo.cs:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "xmlFile.xml", Watch = true)]

xsl
+7  A: 

You can bind in a schema to the log4net element. There are a few floating around, most do not fully provide for the various options avialable. I create the following xsd to provide as much verification as possible: http://csharptest.net/downloads/schema/log4net.xsd

You can bind it into the xml easily by modifying the log4net element: <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Very strange that log4net doesn't include an XSD.
James McMahon
Didn't actually get around to trying this for awhile, I couldn't get it to work under visual studio 2005. Well it works, but it still throws warnings about all the XML elements.
James McMahon
+2  A: 

Just a word of warning to anyone follow the advice of the answers in this thread. There is a possible security risk by having the log4net configuration in an xml off the root of the web service, as it will be accessible to anyone by default. Just be advised if your configuration contains sensitive data, you may want to put it else where.

James McMahon
+2  A: 

For VS2008 just add the log4net.xsd file to your project; VS looks in the project folder as well as the installation directory that Wheelie mentioned.

Also, using a .config extension instead of .xml avoids the security issue since IIS doesn't serve *.config files by default.

devstuff
A: 

Actually you don't need to stick to the .xml extension. You can specify any other extension in the ConfigFileExtension attribute:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension=".config", Watch = true)]
medvekoma
+1  A: 

I got a test asp projec to build by puting the xsd file in the visual studio schemas folder as described above (for me it is C:\Program Files\Microsoft Visual Studio 8\XML\Schemas) and then making my web.config look like this:

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\Config 
-->
<configuration>
  <configSections>


    <section  name="log4net" 
              type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

  </configSections>
  <appSettings>

  </appSettings>
  <connectionStrings>

  </connectionStrings>
  <system.web>
    <trace enabled="true" pageOutput="true"  />
    <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
    <compilation debug="true" />
    <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
    <authentication mode="Windows" />

    <customErrors mode="Off"/>
    <!--
      <customErrors mode="Off"/>

            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

    <customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
      <error statusCode="403" redirect="NoAccess.htm" />
      <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
        -->





  </system.web>
    <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <!-- Please make shure the ..\\Logs directory exists! -->
      <param name="File" value="Logs\\Log4Net.log"/>
      <!--<param name="AppendToFile" value="true"/>-->
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="" />
      <from value="" />
      <subject value="" />
      <smtpHost value="" />
      <bufferSize value="512" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property] - %message%newline%newline%newline" />
      </layout>
    </appender>

    <logger name="File">
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
    </logger>
    <logger name="EmailLog">
      <level value="ALL" />
      <appender-ref ref="SmtpAppender" />
    </logger>
  </log4net>
</configuration>