log4net: Error on loading custom appender

2019-05-03 03:12发布

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>

6条回答
唯我独甜
2楼-- · 2019-05-03 03:36

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?

查看更多
Deceive 欺骗
3楼-- · 2019-05-03 03:39

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.

查看更多
forever°为你锁心
4楼-- · 2019-05-03 03:44

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

查看更多
够拽才男人
6楼-- · 2019-05-03 03:50

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>
查看更多
SAY GOODBYE
7楼-- · 2019-05-03 03:52

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.

查看更多
登录 后发表回答