Log4Net “Could not find schema information” messages

后端 未结 13 798
故里飘歌
故里飘歌 2020-12-01 04:17

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 usi

相关标签:
13条回答
  • 2020-12-01 04:33

    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.

    0 讨论(0)
  • 2020-12-01 04:36

    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.

    0 讨论(0)
  • 2020-12-01 04:36

    I noticed it a bit late, but if you look into the examples log4net furnishes you can see them put all of the configuration data into an app.config, with one difference, the registration of configsection:

    <!-- Register a section handler for the log4net section -->
    <configSections>
        <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
    </configSections>
    

    Could the definition it as type "System.Configuration.IgnoreSectionHandler" be the reason Visual Studio does not show any warning/error messages on the log4net stuff?

    0 讨论(0)
  • 2020-12-01 04:39

    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

    0 讨论(0)
  • 2020-12-01 04:39

    In Roger's answer, where he provided a schema, this worked very well for me except where a commenter mentioned

    This XSD is complaining about the use of custom appenders. It only allows for an appender from the default set (defined as an enum) instead of simply making this a string field

    I modified the original schema which had a xs:simpletype named log4netAppenderTypes and removed the enumerations. I instead restricted it to a basic .NET typing pattern (I say basic because it just supports typename only, or typename, assembly -- however someone can extend it.

    Simply replace the log4netAppenderTypes definition with the following in the XSD:

    <xs:simpleType name="log4netAppenderTypes">
      <xs:restriction base="xs:string">
        <xs:pattern value="[A-Za-z_]\w*(\.[A-Za-z_]\w*)+(\s*,\s*[A-Za-z_]\w*(\.[A-Za-z_]\w*)+)?"/>
      </xs:restriction>
    </xs:simpleType>
    

    I'm passing this back on to the original author if he wants to include it in his official version. Until then you'd have to download and modify the xsd and reference it in a relative manner, for example:

    <log4net
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="../../../Dependencies/log4net/log4net.xsd">
      <!-- ... -->
    </log4net>
    
    0 讨论(0)
  • 2020-12-01 04:39

    I got a test asp project 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">
          <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>
    
    0 讨论(0)
提交回复
热议问题