NUnit and Log4Net Integration: Log-Based Approval

This is my first stack overflow question. I was not lucky to find the answer via google or stackoverflow.

I am interested in the nunit test looking at log4net for a specific log entry and claiming based on the results of this search.

Based on an unrelated message, I read re: log4net, I think that maybe I can use MemoryAppender to do this using the GetEvents method and looking at the array of returned events.

But I wonder: 1. Has anyone done this? Any pitfalls or suggestions? Any alternative approaches? 2. Does anyone have a recipe that they could submit?

Thank.

+3
source share
1 answer

Well, answering my own question (more precisely, my colleague, thanks Beth).

In your configuration for log4net: configuration:

<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 an array of messages, do what you want.

This does not apply if you have an existing log file and you want to search for it.

+3
source

Source: https://habr.com/ru/post/1729284/


All Articles