Created
September 12, 2013 13:31
-
-
Save JakeGinnivan/6537351 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [TestFixture] | |
| public class RecordTest | |
| { | |
| [Test] | |
| public void DoTest() | |
| { | |
| var testing = Log4NetTestHelper.RecordLog(() => | |
| { | |
| var log = LogManager.GetLogger(typeof (RecordTest)); | |
| log.Error("Testing!"); | |
| }); | |
| Assert.AreEqual("Testing!", testing[0]); | |
| } | |
| } | |
| public static class Log4NetTestHelper | |
| { | |
| public static string[] RecordLog(Action action) | |
| { | |
| if (!LogManager.GetRepository().Configured) | |
| BasicConfigurator.Configure(); | |
| var logMessages = new List<string>(); | |
| var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root; | |
| var attachable = root as IAppenderAttachable; | |
| var appender = new MemoryAppender(); | |
| if (attachable != null) | |
| attachable.AddAppender(appender); | |
| action(); | |
| var loggingEvents = appender.GetEvents(); | |
| foreach (var loggingEvent in loggingEvents) | |
| { | |
| var stringWriter = new StringWriter(); | |
| loggingEvent.WriteRenderedMessage(stringWriter); | |
| logMessages.Add(stringWriter.ToString()); | |
| } | |
| if (attachable != null) | |
| attachable.RemoveAppender(appender); | |
| return logMessages.ToArray(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment