Ukadc.Diagnostics.Listeners.InMemoryTraceListener
Description
The InMemoryTraceListener is a very simple TraceListener extends the
CustomTraceListener to create a listener that is useful for unit testing.
When the InMemoryTraceListener is wired to a trace source and receives log events they are added to a static collection of InMemoryTraceObjects that can be inspected at runtime from anywhere within the same appdomain.
Example Usage
Here's an example unit test using the InMemoryTraceListener
[TestMethod]
public void WriteBasicMessage()
{
TraceSource source = new TraceSource("TestSource");
source.Listeners.Add(new InMemoryTraceListener());
source.Switch = new SourceSwitch("test") { Level = SourceLevels.All };
InMemoryTraceListener.TraceObjects.Clear();
source.TraceInformation("Basic test");
Assert.AreEqual("Basic test", InMemoryTraceListener.TraceObjects[0].Message);
}
This listener is used extensively in the unit tests that accompany the UKAdc.Diagnostics library to test other features such as the Filters and wrapping APIs.
Important: Notice that the TraceObjects collection is static and therefore will be the repository for all of your unit tests! Therefore, it might be wise to call
InMemoryTraceListener.TraceObjects.Clear() at the start of each test or in your TestInitialize method.