public void TestReceivedLogEventContextTest() { using (var loggerScope = new InternalLoggerScope()) { // Arrange var targetContext = new NLog.Targets.DebugTarget() { Name = "Ugly" }; var receivedArgs = new List <InternalLoggerMessageEventArgs>(); InternalLogger.LogMessageReceived += (sender, e) => { receivedArgs.Add(e); }; var exception = new Exception(); // Act NLog.Internal.ExceptionHelper.MustBeRethrown(exception, targetContext); // Assert Assert.Single(receivedArgs); var logEventArgs = receivedArgs.Single(); Assert.Equal(LogLevel.Error, logEventArgs.Level); Assert.Equal(exception, logEventArgs.Exception); Assert.Equal(targetContext.GetType(), logEventArgs.SenderType); } }
public void TestReceivedLogEventTest() { using (var loggerScope = new InternalLoggerScope()) { // Arrange var receivedArgs = new List <InternalLoggerMessageEventArgs>(); var logFactory = new LogFactory(); logFactory.Setup().SetupInternalLogger(s => { EventHandler <InternalLoggerMessageEventArgs> eventHandler = (sender, e) => receivedArgs.Add(e); s.AddLogSubscription(eventHandler); s.RemoveLogSubscription(eventHandler); s.AddLogSubscription(eventHandler); }); var exception = new Exception(); // Act InternalLogger.Info(exception, "Hello {0}", "it's me!"); // Assert Assert.Single(receivedArgs); var logEventArgs = receivedArgs.Single(); Assert.Equal(LogLevel.Info, logEventArgs.Level); Assert.Equal(exception, logEventArgs.Exception); Assert.Equal("Hello it's me!", logEventArgs.Message); } }
public void WriteToConsoleErrorTests() { using (var loggerScope = new InternalLoggerScope(true)) { // Expected result is the same for both types of method invocation. const string expected = "Warn WWW\nError EEE\nFatal FFF\nTrace TTT\nDebug DDD\nInfo III\n"; InternalLogger.LogLevel = LogLevel.Trace; InternalLogger.IncludeTimestamp = false; InternalLogger.LogToConsoleError = true; { StringWriter consoleWriter1 = new StringWriter() { NewLine = "\n" }; // Redirect the console output to a StringWriter. loggerScope.SetConsoleError(consoleWriter1); // Named (based on LogLevel) public methods. InternalLogger.Warn("WWW"); InternalLogger.Error("EEE"); InternalLogger.Fatal("FFF"); InternalLogger.Trace("TTT"); InternalLogger.Debug("DDD"); InternalLogger.Info("III"); TestWriter(expected, loggerScope.ConsoleErrorWriter); } { // // Redirect the console output to another StringWriter. StringWriter consoleWriter2 = new StringWriter() { NewLine = "\n" }; loggerScope.SetConsoleError(consoleWriter2); // Invoke Log(LogLevel, string) for every log level. InternalLogger.Log(LogLevel.Warn, "WWW"); InternalLogger.Log(LogLevel.Error, "EEE"); InternalLogger.Log(LogLevel.Fatal, "FFF"); InternalLogger.Log(LogLevel.Trace, "TTT"); InternalLogger.Log(LogLevel.Debug, "DDD"); InternalLogger.Log(LogLevel.Info, "III"); TestWriter(expected, loggerScope.ConsoleErrorWriter); } } }
public void InternalLoggingConfigTest1() { using (var scope = new InternalLoggerScope()) { CreateConfigurationFromString(@" <nlog internalLogFile='c:\file.txt' internalLogLevel='Trace' internalLogToConsole='true' internalLogToConsoleError='true' globalThreshold='Warn' throwExceptions='true'> </nlog>"); Assert.Same(LogLevel.Trace, InternalLogger.LogLevel); Assert.True(InternalLogger.LogToConsole); Assert.True(InternalLogger.LogToConsoleError); Assert.Same(LogLevel.Warn, LogManager.GlobalThreshold); Assert.True(LogManager.ThrowExceptions); } }
public void InternalLoggingConfigTest1() { using (var scope = new InternalLoggerScope()) { CreateConfigurationFromString(@" <nlog internalLogFile='c:\file.txt' internalLogLevel='Trace' internalLogToConsole='true' internalLogToConsoleError='true' globalThreshold='Warn' throwExceptions='true'> </nlog>"); Assert.Same(LogLevel.Trace, InternalLogger.LogLevel); Assert.True(InternalLogger.LogToConsole); Assert.True(InternalLogger.LogToConsoleError); Assert.Same(LogLevel.Warn, LogManager.GlobalThreshold); Assert.True(LogManager.ThrowExceptions); } }
public void TestReceivedLogEventTest() { using (var loggerScope = new InternalLoggerScope()) { // Arrange var receivedArgs = new List <InternalLoggerMessageEventArgs>(); InternalLogger.LogMessageReceived += (sender, e) => { receivedArgs.Add(e); }; var exception = new Exception(); // Act InternalLogger.Info(exception, "Hello {0}", "it's me!"); // Assert Assert.Single(receivedArgs); var logEventArgs = receivedArgs.Single(); Assert.Equal(LogLevel.Info, logEventArgs.Level); Assert.Equal(exception, logEventArgs.Exception); Assert.Equal("Hello it's me!", logEventArgs.Message); } }