代码之家  ›  专栏  ›  技术社区  ›  xsl Fredrik Hedblad

Log4Net“找不到架构信息”消息

  •  56
  • xsl Fredrik Hedblad  · 技术社区  · 16 年前

    我决定用 log4net web.config :

    找不到的架构信息

    以下是我的报告的相关部分 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>
    

    解决了的:

    1. xml -文件。确保使用 .xml 作为文件扩展名。
    2. 将以下行添加到 AssemblyInfo.cs :
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "xmlFile.xml", Watch = true)]
    

    nemo

    只是给任何人一个警告 遵循中答案的建议 这根线。有一种可能性 使用log4net存在安全风险 根目录下的xml中的配置 web服务的一部分,它将是 默认情况下,任何人都可以访问。只是 如果您的配置 把它放在别的地方。


    @wcm:我尝试使用一个单独的文件。我将以下行添加到 AssemblyInfo.cs

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

    13 回复  |  直到 8 年前
        1
  •  75
  •   Kevin Panko Matthew Woodard    11 年前

    您可以在架构中绑定到 log4net 要素有一些浮动,大多数不完全提供各种可用的选项。我创建了以下xsd以提供尽可能多的验证: http://csharptest.net/downloads/schema/log4net.xsd

    log4net 要素:

    <log4net 
         xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
        2
  •  17
  •   Grault    4 年前

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

    blog post ,它帮助了我。

        3
  •  11
  •   James McMahon    16 年前

        4
  •  5
  •   Wheelie    16 年前

        5
  •  4
  •   Community CDub    8 年前

    在里面 Roger's answer ,他提供了一个模式,这对我来说非常有效,除了一位评论员提到的

    这个XSD抱怨使用自定义附加器。它只允许来自默认集合(定义为枚举)的追加器,而不是简单地将其设置为字符串字段

    xs:simpletype 命名 log4netAppenderTypes 并删除了枚举。相反,我将其限制为基本的.NET键入模式(我之所以说基本是因为它只支持 类别名 只有,或者 类型名称,程序集

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

    如果原著作者想把它包括在他的官方版本中,我会把它传回去。在此之前,您必须下载并修改xsd,并以相对方式引用它,例如:

    <log4net
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="../../../Dependencies/log4net/log4net.xsd">
      <!-- ... -->
    </log4net>
    
        6
  •  3
  •   medvekoma medvekoma    16 年前

    实际上,您不需要坚持使用.xml扩展名。您可以在ConfigFileExtension属性中指定任何其他扩展:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension=".config", Watch = true)]
    
        7
  •  2
  •   xsl Fredrik Hedblad    16 年前

    @steve_mtl:将文件扩展名从 .config .xml 解决了这个问题。非常感谢。

    @Wheelie:我不能尝试你的建议,因为我需要一个可以与未修改的VisualStudio安装一起工作的解决方案。


    1. 将每个log4net特定标记复制到单独的 xml -文件。确保使用 .xml 作为文件扩展名。
    2. 将以下行添加到 AssemblyInfo.cs

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

        8
  •  2
  •   devstuff    16 年前

    对于VS2008,只需将log4net.xsd文件添加到项目中;VS会查看项目文件夹以及Wheelie提到的安装目录。

    此外,使用.config扩展名而不是.xml可以避免安全问题,因为默认情况下IIS不提供*.config文件。

        9
  •  1
  •   wcm    16 年前

        10
  •  1
  •   Philipp M    11 年前

    我得到了一个要构建的测试asp项目,方法是如上所述将xsd文件放在visual studio schemas文件夹中(对我来说是C:\Program Files\Microsoft visual studio 8\XML\schemas),然后 web.config

    <?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>
    
        11
  •  0
  •   Matt Enright    14 年前

        12
  •  0
  •   Fysicus    10 年前

    我注意到有点晚了,但如果您查看log4net提供的示例,您可以看到它们将所有配置数据放入app.config中,其中一个区别是,注册configsection:

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

    将其定义为类型“System.Configuration.IgnoreSectionHandler”是否是VisualStudio在log4net文件上不显示任何警告/错误消息的原因?

        13
  •  0
  •   Community CDub    8 年前

    Kit 我的回答 https://stackoverflow.com/a/11780781/6139051 而且它不适用于AppenderType值,如“log4net.Appender.TraceAppender,log4net”。log4net.dll程序集的AssemblyTitle为“log4net”,即程序集名称中没有点,这就是为什么工具包中的正则表达式无法工作的原因。我必须在regexp中的第三个插入组后面添加问号,然后它就完美地工作了。

    修改后的正则表达式如下所示:

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