Created
October 16, 2012 12:59
-
-
Save mattanja/3899118 to your computer and use it in GitHub Desktop.
log4net TestContextAppender
This file contains 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
using log4net.Appender; | |
using log4net.Core; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System; | |
namespace XXX.Framework.Logging | |
{ | |
/// <summary> | |
/// Simple appender, writing all logging events to the given <c>TestContext</c>. | |
/// </summary> | |
public sealed class TestContextAppender : AppenderSkeleton | |
{ | |
#region Fields | |
// ---------------------------------------------------------------------------------------- | |
// Fields | |
// ---------------------------------------------------------------------------------------- | |
public const string DefaultAppenderName = "TestContextAppender"; | |
// ---------------------------------------------------------------------------------------- | |
#endregion // Fields | |
#region constructor | |
/// <summary> | |
/// Creates an instance of the class <c>TestContextAppender</c>. | |
/// </summary> | |
/// <param name="testContext">The current <c>TestContext</c> as target for logging events.</param> | |
/// <param name="appenderName">Name of the appender.</param> | |
public TestContextAppender(TestContext testContext, string appenderName = DefaultAppenderName) | |
: base() | |
{ | |
this.TestContext = testContext; | |
this.TestContext.Properties[appenderName] = this; | |
} | |
#endregion | |
#region IAppender members | |
/// <summary> | |
/// Write a logging event to the <c>TestContext</c>. | |
/// </summary> | |
/// <param name="loggingEvent">The logging event to write to the <c>TestContext</c>.</param> | |
protected override void Append(LoggingEvent loggingEvent) | |
{ | |
var message = String.Format( | |
"Log [{0}]: {1}, Exception: {2}", | |
loggingEvent.Level, | |
loggingEvent.RenderedMessage, | |
loggingEvent.ExceptionObject | |
); | |
this.TestContext.WriteLine(message); | |
this.LoggingOutput += message; | |
if (loggingEvent.Level >= Level.Warn) | |
{ | |
this.IsWarningOrErrorLogged = true; | |
} | |
} | |
#endregion | |
#region TestContextAppender members | |
// ---------------------------------------------------------------------------------------- | |
// TestContextAppender members | |
// ---------------------------------------------------------------------------------------- | |
/// <summary> | |
/// Gets the instance of the appender from the test context. | |
/// </summary> | |
/// <param name="testContext">The test context.</param> | |
/// <param name="appenderName">Name of the appender.</param> | |
/// <returns></returns> | |
public static TestContextAppender GetFromTestContext(TestContext testContext, string appenderName = DefaultAppenderName) | |
{ | |
return testContext.Properties[appenderName] as TestContextAppender; | |
} | |
/// <summary> | |
/// Asserts the default checks: | |
/// * The logger has been initialized. | |
/// * No warning or error has been logged. | |
/// For further tests get an instance with GetFromTestContext(). | |
/// </summary> | |
/// <param name="testContext">The test context.</param> | |
/// <param name="appenderName">Name of the appender.</param> | |
public static void AssertDefaults(TestContext testContext, string appenderName = DefaultAppenderName) | |
{ | |
var self = GetFromTestContext(testContext, appenderName); | |
Assert.IsNotNull(self); | |
self.AssertNoWarningOrErrorLogged(); | |
} | |
/// <summary> | |
/// Gets or sets the test context. | |
/// </summary> | |
/// <value> | |
/// The test context. | |
/// </value> | |
public TestContext TestContext { get; set; } | |
/// <summary> | |
/// Gets or sets a value indicating whether this instance is warning or error logged. | |
/// </summary> | |
/// <value> | |
/// <c>true</c> if this instance is warning or error logged; otherwise, <c>false</c>. | |
/// </value> | |
public bool IsWarningOrErrorLogged { get; set; } | |
/// <summary> | |
/// Gets or sets the logging output. | |
/// </summary> | |
/// <value> | |
/// The logging output. | |
/// </value> | |
public string LoggingOutput { get; set; } | |
/// <summary> | |
/// Asserts that no warning or error has been logged. | |
/// </summary> | |
public void AssertNoWarningOrErrorLogged() | |
{ | |
Assert.IsFalse(this.IsWarningOrErrorLogged, String.Format("Error or warnings have been logged: {0}", this.LoggingOutput)); | |
} | |
/// <summary> | |
/// Asserts the logging output not empty. | |
/// </summary> | |
public void AssertLoggingOutputNotEmpty() | |
{ | |
Assert.IsFalse(String.IsNullOrEmpty(this.LoggingOutput), String.Format("The logging output is not empty: {0}", this.LoggingOutput)); | |
} | |
// ---------------------------------------------------------------------------------------- | |
#endregion // TestContextAppender members | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment