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:
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
var attachable = root as IAppenderAttachable;
var appender = new log4net.Appender.MemoryAppender();
if(attachable!=null)
attachable.AddAppender(appender);
// do stuff
var loggingEvents = appender.GetEvents();
foreach (var loggingEvent in loggingEvents)
loggingEvent.WriteRenderedMessage(writer);
if(attachable!=null)
attachable.RemoveAppender(appender);
...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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…