public void LogMessageCallbackTest()
        {
            StatusLogger.MessageLogged += MessageLoggedCallback;

            const string messageText = "Test Message";
            const string messageSource = "MessageSource";
            const MessageType messageType = MessageType.Error;
            const string innerExceptionText = "Inner Exception TEST";
            const string exceptionText = "This is an exception.";
            ArgumentNullException exception = new ArgumentNullException(exceptionText, new ArgumentException(innerExceptionText));
            Message messageToLog = new Message(messageText, messageSource, messageType, exception);

            StatusLogger.LogMessage(messageToLog);

            Assert.IsTrue(resetEvent.WaitOne(TimeoutValue));
            Assert.IsNotNull(messageReceived);
            Assert.AreEqual(messageText, messageReceived.Text);
            Assert.AreEqual(messageSource, messageReceived.Source);
            Assert.AreEqual(messageType, messageReceived.Level);
            Assert.AreEqual(exceptionText, messageReceived.Exception.Message);
            Assert.AreEqual(innerExceptionText, messageReceived.Exception.InnerException.Message);

            // Test that another message is not logged if unsubscribed
            resetEvent.Reset();
            StatusLogger.MessageLogged -= MessageLoggedCallback;
            StatusLogger.LogMessage(messageToLog);
            Assert.IsFalse(resetEvent.WaitOne(TimeoutValue));
        }
        public void LogMessageTest()
        {
            try { StatusLogger.LogMessage(null); }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(ArgumentNullException));
            }

            const string messageText = "Test Message";
            const string messageSource = "MessageSource";
            const MessageType messageType = MessageType.Information;
            Message message = new Message(messageText, messageSource, messageType);

            StatusLogger.LogMessage(message);

            const string innerExceptionText = "Inner Exception TEST";
            const string exceptionText = "This is an exception.";
            Message message2 = new Message(messageText, messageSource, messageType, new ArgumentNullException(exceptionText, new ArgumentException(innerExceptionText)));
            StatusLogger.LogMessage(message2);

            Thread.Sleep(100);
            StatusLogger_Accessor.Shutdown();
            Thread.Sleep(100);

            Assert.IsTrue(File.Exists(StatusLogger_Accessor.logFilePath));

            using (FileStream stream = new FileStream(StatusLogger_Accessor.logFilePath, FileMode.Open, FileAccess.Read))
            using (StreamReader reader = new StreamReader(stream))
            {
                string line = reader.ReadLine();
                Assert.IsFalse(string.IsNullOrWhiteSpace(line));

                string[] values = line.Split(new [] {'\t'}, StringSplitOptions.RemoveEmptyEntries);
                Assert.AreEqual(4, values.Length);
                Assert.AreEqual(messageType.ToString(), values[1].Trim());
                Assert.AreEqual(messageSource, values[2].Trim());
                Assert.AreEqual(messageText, values[3].Trim());

                string line2 = reader.ReadLine();
                Assert.IsFalse(string.IsNullOrWhiteSpace(line2));

                string[] values2 = line2.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                Assert.AreEqual(4, values2.Length);
                Assert.AreEqual(messageType.ToString(), values2[1].Trim());
                Assert.AreEqual(messageSource, values2[2].Trim());
                Assert.AreEqual(messageText, values2[3].Trim());

                string remainder = reader.ReadToEnd();
                Assert.IsTrue(remainder.Contains("Exception: "));
                Assert.IsTrue(remainder.Contains(" Inner Exception: "));
                Assert.IsTrue(remainder.Contains(" Stack Trace: "));
                Assert.IsTrue(remainder.Contains(innerExceptionText));
                Assert.IsTrue(remainder.Contains(exceptionText));
            }
        }
 /// <summary>
 /// Called when a message is logged.
 /// </summary>
 /// <param name="message">Logged message</param>
 private void MessageLoggedCallback(Message message)
 {
     messageReceived = message;
     resetEvent.Set();
 }
        public void MyTestCleanup()
        {
            messageReceived = null;
            StatusLogger_Accessor.Shutdown();

            StatusLogger_Accessor.tokenSource = new CancellationTokenSource();
            StatusLogger_Accessor.messageQueue = new System.Collections.Concurrent.ConcurrentQueue<Message>();
            StatusLogger_Accessor.messageConsumeTask = null;

            // Allow time for the thread to stop
            Thread.Sleep(500);

            string logFile = StatusLogger_Accessor.logFilePath;
            if (File.Exists(logFile))
            {
                File.Delete(logFile);
            }
        }