NUnit 和 Log4Net 集成:基于日志断言

发布于 2024-08-18 17:44:47 字数 270 浏览 4 评论 0原文

这是我关于堆栈溢出的第一个问题。我没有太多运气通过谷歌或 stackoverflow 找到答案。

我感兴趣的是让 nunit 测试检查 log4net 中日志中的特定条目,并根据搜索结果进行断言。

根据我阅读的一篇不相关的文章:log4net,我想我可以使用 MemoryAppender 通过 GetEvents 方法并仔细阅读返回的事件数组来完成此操作。

但我想知道: 1.有人做过吗?有什么陷阱或建议吗?还有其他方法吗? 2. 有谁可以提供食谱吗?

谢谢。

This is my first question on stack overflow. I haven't had much luck finding an answer via google or stackoverflow.

I'm interested in having an nunit test examine the log4net for a specific entry in the log and assert based on the results of that search.

Based on an unrelated post I read re: log4net, I think I can probably use MemoryAppender to do this via the GetEvents method and perusing the array of events returned.

But I'm wondering:
1. Has anyone done this? Any pitfalls or suggestions? Any alternate approaches?
2. Does any have a recipe they could present?

Thanks.

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

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

发布评论

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

评论(1

冬天的雪花 2024-08-25 17:44:47

好的,回答我自己的问题(或者更确切地说,我的同事是,谢谢贝丝)。

在 log4net 的配置中:
配置:

<log4net>
     <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender" />
     <root>
     <level value="DEBUG" />
     <appender-ref ref="MemoryAppender" />
     </root>
</log4net>

在您的 .net 代码中:

List<string> messages = new List<string>();
Hierarchy hierarchy = LogManager.GetLoggerRepository() as Hierarchy;
MemoryAppender appender = hierarchy.Root.GetAppender("MemoryAppender") as MemoryAppender;
LoggingEvent[] eventList = appender.GetEvents();

foreach (LoggingEvent item in eventList)
   messages.Add(item.RenderedMessage);

return messages.ToArray();

一旦获得消息数组,就可以执行您想要的操作。

如果您有现有日志文件并想要搜索它,则这无法解决。

Ok, answering my own question (or rather, my co-worker is, thanks Beth).

In your configuration for log4net:
config:

<log4net>
     <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender" />
     <root>
     <level value="DEBUG" />
     <appender-ref ref="MemoryAppender" />
     </root>
</log4net>

In your .net code:

List<string> messages = new List<string>();
Hierarchy hierarchy = LogManager.GetLoggerRepository() as Hierarchy;
MemoryAppender appender = hierarchy.Root.GetAppender("MemoryAppender") as MemoryAppender;
LoggingEvent[] eventList = appender.GetEvents();

foreach (LoggingEvent item in eventList)
   messages.Add(item.RenderedMessage);

return messages.ToArray();

Once you have the messages array, do what you want to.

This doesn't address if you have an existing log file and want to search it.

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