Log4Net“无法找到架构信息”消息
-
05-07-2019 - |
题
我决定使用 log4net 作为新的Web服务项目的记录器。一切都运行正常,但是我在 web.config
中使用的每个log4net标签都收到了很多类似下面的消息:
无法找到架构信息 元素'log4net'......
以下是 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>
解决:
- 将每个log4net特定标记复制到单独的
xml
-file。确保使用.xml
作为文件扩展名。 - 将以下行添加到
AssemblyInfo.cs
:
醇>
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "xmlFile.xml", Watch = true)]
nemo 补充道:
向任何人发出警告 遵循答案中的建议 这个帖子。有可能 具有log4net的安全风险 关闭root的xml配置 Web服务,就像它一样 任何人都可以默认访问。只是 如果你的配置,请告知 包含敏感数据,您可能需要 把它放在哪里。
@wcm:我尝试使用单独的文件。我将以下行添加到 AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
并将处理log4net的所有内容放在该文件中,但我仍然收到相同的消息。
解决方案
我有不同的看法,需要以下语法:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.xml", Watch = true)]
与xsl的上一篇文章不同,但对我有所帮助。看看这个博客文章,它帮助了我。
其他提示
您可以在架构中绑定 log4net
元素。有几个漂浮在周围,大多数没有完全提供各种可用选项。我创建了以下xsd以提供尽可能多的验证:
http://csharptest.net/downloads/schema/log4net.xsd
您可以通过修改 log4net
元素轻松地将其绑定到xml中:
<log4net
xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
只需向任何人发出警告,请遵循此主题中答案的建议。将x4中的log4net配置放在Web服务的根目录下可能存在安全风险,因为默认情况下任何人都可以访问它。如果您的配置包含敏感数据,请注意,您可能希望将其放在哪里。
我相信您正在看到该消息,因为Visual Studio不知道如何验证配置文件的log4net部分。您应该能够通过将log4net XSD复制到C:\ Program Files \ Microsoft Visual Studio 8 \ XML \ Schemas(或安装Visual Studio的任何位置)来解决此问题。作为额外的奖励,您现在应该获得对log4net的智能感知支持
实际上,您不需要坚持.xml扩展名。您可以在ConfigFileExtension属性中指定任何其他扩展名:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension=".config", Watch = true)]
@steve_mtl:将文件扩展名从 .config
更改为 .xml
解决了这个问题。谢谢。
@Wheelie:我无法尝试你的建议,因为我需要一个适用于未经修改的Visual Studio安装的解决方案。
总结一下,以下是解决问题的方法:
- 将每个log4net特定标记复制到单独的
xml
-file。确保使用.xml
作为文件扩展名。 -
将以下行添加到
AssemblyInfo.cs
:[assembly:log4net.Config.XmlConfigurator(ConfigFile =&quot; xmlFile.xml&quot;,Watch = true)]
醇>
对于VS2008,只需将log4net.xsd文件添加到项目中; VS查找项目文件夹以及Wheelie提到的安装目录。
此外,使用.config扩展名而不是.xml可以避免安全问题,因为IIS默认情况下不提供* .config文件。
在 Roger的回答中,他提供了一个架构,除了评论者提到的内容之外,这对我来说非常有效
这个XSD抱怨使用自定义appender。它只允许默认集(定义为枚举)中的appender而不是简单地将其设为字符串字段
我修改了原始模式,其中 xs:simpletype
命名为 log4netAppenderTypes
并删除了枚举。我改为将其限制为基本的.NET输入模式(我说基本因为它只支持 typename ,或 typename,assembly - 但是有人可以扩展它。
只需在XSD中使用以下内容替换 log4netAppenderTypes
定义:
<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>
您是否尝试过使用单独的log4net.config文件?
我通过将xsd文件放在visual studio schemas文件夹中来构建一个测试asp项目,如上所述(对我来说它是C:\ Program Files \ Microsoft Visual Studio 8 \ XML \ Schemas)然后制作我的< code> 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>
不修改Visual Studio安装,并考虑正确的版本控制等。在团队的其他成员中,将.xsd文件添加到您的解决方案中(作为“解决方案项目”),或者如果您只想将其用于特定项目,只需将其嵌入到那里。
我注意到它有点晚了,但如果你看一下log4net提供的例子,你可以看到他们把所有配置数据放到app.config中,但有一点不同,就是注册configsection:
<!-- Register a section handler for the log4net section -->
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
可以将其定义为类型“System.Configuration.IgnoreSectionHandler”是Visual Studio在log4net上没有显示任何警告/错误消息的原因吗?
我按照 Kit 的回答 https://stackoverflow.com/a/11780781/6139051 它不适用于像”log4net.Appender.TraceAppender,log4net“这样的AppenderType值。 log4net.dll程序集的AssemblyTitle为“log4net”,即程序集名称里面没有一个点,这就是为什么Kit的答案中的正则表达式不起作用的原因。我必须在正则表达式中的第三个括号组之后添加问号,然后才能完美地工作。
修改后的正则表达式如下所示:
<xs:pattern value="[A-Za-z_]\w*(\.[A-Za-z_]\w*)+(\s*,\s*[A-Za-z_]\w*(\.[A-Za-z_]\w*)?+)?"/>