protected void ForwardInternalError(string message, Exception exception, Type thisType) { LogLog.Error(thisType, message, exception); var loggingEvent = loggingEventHelper.CreateLoggingEvent(Level.Error, message, exception); ForwardLoggingEvent(loggingEvent, thisType); }
public void HelperCanGenerateLoggingEventWithAllProperties() { // Arrange var helper = new LoggingEventHelper("TestLoggerName", FixFlags.All); ThreadContext.Properties["MyProperty"] = "MyValue"; var exception = new Exception("SomeError"); var stackFrame = new StackFrame(0); var currentUser = WindowsIdentity.GetCurrent(); var loggingTime = DateTime.Now; // Log4Net does not seem to be using UtcNow // Act var loggingEvent = helper.CreateLoggingEvent(Level.Emergency, "Who's on live support?", exception); Thread.Sleep(50); // to make sure the time stamp is actually captured // Assert Assert.That(loggingEvent.Domain, Is.EqualTo(AppDomain.CurrentDomain.FriendlyName), "Domain"); Assert.That(loggingEvent.Identity, Is.Empty, "Identity: always empty for some reason"); Assert.That(loggingEvent.UserName, Is.EqualTo(currentUser == null ? String.Empty : currentUser.Name), "UserName"); Assert.That(loggingEvent.ThreadName, Is.EqualTo(Thread.CurrentThread.Name), "ThreadName"); Assert.That(loggingEvent.Repository, Is.Null, "Repository: Helper does not have access to this"); Assert.That(loggingEvent.LoggerName, Is.EqualTo("TestLoggerName"), "LoggerName"); Assert.That(loggingEvent.Level, Is.EqualTo(Level.Emergency), "Level"); Assert.That(loggingEvent.TimeStamp, Is.EqualTo(loggingTime).Within(TimeSpan.FromMilliseconds(5)), "TimeStamp"); Assert.That(loggingEvent.ExceptionObject, Is.EqualTo(exception), "ExceptionObject"); Assert.That(loggingEvent.MessageObject, Is.EqualTo("Who's on live support?"), "MessageObject"); Assert.That(loggingEvent.LocationInformation.MethodName, Is.EqualTo(stackFrame.GetMethod().Name), "LocationInformation"); Assert.That(loggingEvent.Properties["MyProperty"], Is.EqualTo("MyValue"), "Properties"); }
public void HelperCanGenerateLoggingEventWithAllProperties() { // Arrange var helper = new LoggingEventHelper("TestLoggerName", FixFlags.All); ThreadContext.Properties["MyProperty"] = "MyValue"; var exception = new Exception("SomeError"); var stackFrame = new StackFrame(0); var currentUser = WindowsIdentity.GetCurrent(); var loggingTime = DateTime.Now; // Log4Net does not seem to be using UtcNow // Act var loggingEvent = helper.CreateLoggingEvent(Level.Emergency, "Who's on live support?", exception); Thread.Sleep(50); // to make sure the time stamp is actually captured // Assert Assert.That(loggingEvent.Domain, Is.EqualTo(AppDomain.CurrentDomain.FriendlyName), "Domain"); //The identity assigned to new threads is dependent upon AppDomain principal policy. //Background information here:http://www.neovolve.com/post/2010/10/21/Unit-testing-a-workflow-that-relies-on-ThreadCurrentPrincipalIdentityName.aspx //VS2013 does have a principal assigned to new threads in the unit test. //It's probably best not to test that the identity has been set. //Assert.That(loggingEvent.Identity, Is.Empty, "Identity: always empty for some reason"); Assert.That(loggingEvent.UserName, Is.EqualTo(currentUser == null ? String.Empty : currentUser.Name), "UserName"); Assert.That(loggingEvent.ThreadName, Is.EqualTo(Thread.CurrentThread.Name), "ThreadName"); Assert.That(loggingEvent.Repository, Is.Null, "Repository: Helper does not have access to this"); Assert.That(loggingEvent.LoggerName, Is.EqualTo("TestLoggerName"), "LoggerName"); Assert.That(loggingEvent.Level, Is.EqualTo(Level.Emergency), "Level"); //Raised time to within 10 ms. However, this may not be a valid test. The time is going to vary from system to system. The //tolerance setting here is arbitrary. Assert.That(loggingEvent.TimeStamp, Is.EqualTo(loggingTime).Within(TimeSpan.FromMilliseconds(10)), "TimeStamp"); Assert.That(loggingEvent.ExceptionObject, Is.EqualTo(exception), "ExceptionObject"); Assert.That(loggingEvent.MessageObject, Is.EqualTo("Who's on live support?"), "MessageObject"); Assert.That(loggingEvent.LocationInformation.MethodName, Is.EqualTo(stackFrame.GetMethod().Name), "LocationInformation"); Assert.That(loggingEvent.Properties["MyProperty"], Is.EqualTo("MyValue"), "Properties"); }