log4net:加载自定义附加程序时出错

发布于 2024-08-18 13:48:56 字数 1534 浏览 28 评论 0原文

我扩展了 AppenderSkeleton 以创建一个名为 HTTPAppender 的自定义附加程序,但 xml 文件中对其的引用出现了问题。 Log4Net 显然无法找到我的自定义附加程序。有没有办法从 xml 文件引用它以指向我的项目,或者我是否必须将自定义附加程序的源代码添加到 log4net 中,以便将其打包在 log4net.dll 中?

调试时,我在“立即”窗口中收到以下错误:

log4net:ERROR XmlHierarchyConfigurator: Could not create Appender [HTTPAppender] of type [HTTPAppender.HTTPAppender,HTTPAppender]. Reported error follows.
System.IO.FileNotFoundException: Could not load file or assembly 'HTTPAppender' or one of its dependencies. The system cannot find the file specified.
File name: 'HTTPAppender'
   at System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
...etc

这是 xml 文件的 log4net 部分:

;; <评估器类型=“log4net.Core.LevelEvaluator,log4net”> <阈值=“警告”/> <布局类型=“log4net.Layout.PatternLayout”><根> <等级值=“全部”/>> <记录器名称=“log4netErrorLog”> <级别值=“调试”/>>

I extended AppenderSkeleton to create a custom appender called HTTPAppender, but something is up with the reference to it in the xml file. Log4Net is clearly unable to find my custom appender. Is there a way to reference it from the xml file to point to my project, or would I have to add my custom appender's source code to log4net's so it's packaged in log4net.dll?

I get the following error in the Immediate window when debugging:

log4net:ERROR XmlHierarchyConfigurator: Could not create Appender [HTTPAppender] of type [HTTPAppender.HTTPAppender,HTTPAppender]. Reported error follows.
System.IO.FileNotFoundException: Could not load file or assembly 'HTTPAppender' or one of its dependencies. The system cannot find the file specified.
File name: 'HTTPAppender'
   at System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
...etc

Here's the log4net section of the xml file:

<log4net>
<appender name="HTTPAppender" type="HTTPAppender.HTTPAppender,HTTPAppender">
<evaluator type="log4net.Core.LevelEvaluator,log4net">
<threshold value="WARN"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="#%level - %message" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="HTTPAppender" />
</root>
<logger name="log4netErrorLog" >
<level value="DEBUG" />
<appender-ref ref="HTTPAppender" />
</logger>
</log4net>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

海之角 2024-08-25 13:48:56

如果您使用任何非标准程序集,请将它们与程序集一起放在应用程序目录中。如果这没有帮助,请尝试为程序集指定一个强名称并在 log4net 配置文件中使用全名。您也可以尝试将其放入 GAC 中。

If you are using any non standard assemblies, put them in the application directory together with your assembly. If that doesn't help, try giving your assembly a strong name and using the full name in the log4net config file. you can also try putting it into GAC.

对风讲故事 2024-08-25 13:48:56

我假设找不到该程序集。程序集的名称确实是“HTTPAppender”吗?它是否与调用程序集位于同一路径中?

I would assume that the assembly cannot be found. Is the name of the assembly indeed 'HTTPAppender' and is it in the same path as the calling assembly?

◇流星雨 2024-08-25 13:48:56

我也遇到过同样的问题。如果您的自定义附加程序项目使用特定的 log4net.dll,并且将要使用自定义附加程序的项目使用不同的 log4net.dll,则它将无法找到它。

I've had the same issue. If your custom appender project uses a certain log4net.dll, and your project that's going to use your custom appender uses a different log4net.dll, it will not be able to find it.

堇色安年 2024-08-25 13:48:56

我最近遇到了同样的问题。我通过在类型的完全限定名称后面添加程序集名称并在类型属性中使用逗号来修复此问题。请看下文。

<log4net>
    <appender name="LogFileAppender" type="TestHarness.Model.CustomRollingFileAppender, TestHarness.Model">
 </log4net>

I run into the same issue recently. I fixed it by adding the assembly name after the fully qualified name of the type with a comma in the type attribute. Please see below.

<log4net>
    <appender name="LogFileAppender" type="TestHarness.Model.CustomRollingFileAppender, TestHarness.Model">
 </log4net>
梦毁影碎の 2024-08-25 13:48:56

我也被这个问题困扰了,最后我解决了。

解决方案是HTTPAppender类应该有一个默认构造函数

我确信这个错误“无法创建Appender”是因为没有默认构造函数而出现的

I was also stuck in that issue finally I fixed it.

The solution is HTTPAppender class should have a Default contructor.

I am confident about that this error "Could not create Appender" is come because doesn't have default constructor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文