以编程方式在 log4net 中添加和删除日志附加程序
我有一个使用 log4net 的组件。我想创建单元测试,以验证某些错误条件是否会导致正确的日志记录。
我认为最好的方法是创建一个 ILogAppender 实现,例如一个模拟。然后,我会在测试设置期间将日志附加程序添加到 log4net,检查测试验证期间写入的内容,并在测试拆卸期间再次将其删除。
这可能吗?
I have a component that uses log4net. I want to create unit tests, that validate that certain error conditions result in the correct logging.
I was thinking that the best way to do this is to create an ILogAppender implementation, for example a mock. I would then add the log appender to log4net during test setup, inspect what was written during test validation, and remove it again during test teardown.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 BasicConfigurator 适合单元测试(OP 要求的内容,但不是主题行中的内容)。其他答案抓取特定记录器的输出。
我想要它全部(这是网站内的“自测试”页面)。最后我基本上做了以下事情:
...但是按照@Pawel的方法包装为一次性的
更新:Pawel的答案已被删除,所以我在这里添加他的链接:
以编程方式检查 Log4Net 日志。
Using the BasicConfigurator is fine for unit testing (what the OP asked for, but not what's in the subject line). The other answers grab output for a specific logger.
I wanted it all (this was a 'self test' page within a website). In the end I did basically the following:
...but wrapped up as a Disposable as per @Pawel's approach
UPDATE: Pawel's answer was deleted, so I am adding his link here:
Programmatically check Log4Net log.
我一直在使用 BasicConfigurator 配置了 MemoryAppender。此附加程序可让您获取测试期间记录的内存中消息。
I have been using the BasicConfigurator configured with a MemoryAppender. This appender lets you get to the in-memory messages logged during your test.
以下代码最初是在 apache 邮件列表档案 并且应该解决在代码中添加和删除 log4net 附加程序的问题
The following code was originally found on the apache mailing list archives and should solve the problem of adding and removing log4net appenders in code
怎么样:
添加相同。
How about:
same for add.