public void CanSendMessageToTransactionalQueue()
        {
            ILogFormatter     formatter = new BinaryLogFormatter();
            MsmqTraceListener listener  =
                new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
                                      MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
                                      false, false, false, MessageQueueTransactionType.Single, new MockMsmqInterfaceFactory());
            LogSource source = new LogSource("unnamed", new[] { listener }, SourceLevels.All);

            MockMsmqInterface.Instance.transactional = true;
            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            source.TraceData(TraceEventType.Error, 1, entry);

            Message message = MockMsmqInterface.Instance.message;

            Assert.IsNotNull(message);
            Assert.IsNotNull(message.Body);
            Assert.AreEqual(message.Body.GetType(), typeof(string));
            Assert.AreEqual(MessageQueueTransactionType.Single, MockMsmqInterface.Instance.transactionType);

            LogEntry deserializedEntry = BinaryLogFormatter.Deserialize(message.Body as string);

            Assert.IsNotNull(deserializedEntry);
            Assert.AreEqual(entry.Message, deserializedEntry.Message);
        }
        public void EnsureIntrinsicPropertiesInitializedAreNotPickedUpDuringDeserialization()
        {
            ILogFormatter     formatter = new BinaryLogFormatter();
            MsmqTraceListener listener  =
                new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
                                      MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
                                      false, false, false, MsmqTraceListenerData.DefaultTransactionType, new MockMsmqInterfaceFactory());
            LogSource source = new LogSource("unnamed", new List <TraceListener>(new TraceListener[] { listener }), SourceLevels.All);

            MockMsmqInterface.Instance.transactional = false;

            Trace.CorrelationManager.ActivityId = Guid.NewGuid();

            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            source.TraceData(TraceEventType.Error, 1, entry);

            // Resetting context before deserialization to ensure that
            // the intrinsic properties picked ud during deserealization
            Trace.CorrelationManager.ActivityId = Guid.Empty;

            Message message = MockMsmqInterface.Instance.message;

            Assert.IsNotNull(message);
            Assert.IsNotNull(message.Body);
            Assert.AreEqual(message.Body.GetType(), typeof(string));

            LogEntry deserializedEntry = BinaryLogFormatter.Deserialize(message.Body as string);

            Assert.IsNotNull(deserializedEntry);

            // Ensure that the deserialized intrinsic properties remain the same
            // as the Log Entry
            Assert.AreEqual(deserializedEntry.ActivityId, entry.ActivityId);
        }
Exemplo n.º 3
0
        public void CanDeserializeFormattedCustomEntry()
        {
            CustomLogEntry entry = new CustomLogEntry();

            entry.TimeStamp    = DateTime.MaxValue;
            entry.Title        = "My custom message title";
            entry.Message      = "My custom message body";
            entry.Categories   = new List <string>(new string[] { "CustomFormattedCategory", "OtherCategory" });
            entry.AcmeCoField1 = "apple";
            entry.AcmeCoField2 = "orange";
            entry.AcmeCoField3 = "lemon";

            string         serializedLogEntryText = new BinaryLogFormatter().Format(entry);
            CustomLogEntry deserializedEntry      =
                (CustomLogEntry)BinaryLogFormatter.Deserialize(serializedLogEntryText);

            Assert.IsNotNull(deserializedEntry);
            Assert.IsFalse(ReferenceEquals(entry, deserializedEntry));
            Assert.AreEqual(entry.Categories.Count, deserializedEntry.Categories.Count);
            foreach (string category in entry.Categories)
            {
                Assert.IsTrue(deserializedEntry.Categories.Contains(category));
            }
            Assert.AreEqual(entry.Message, deserializedEntry.Message);
            Assert.AreEqual(entry.Title, deserializedEntry.Title);
            Assert.AreEqual(entry.AcmeCoField1, deserializedEntry.AcmeCoField1);
            Assert.AreEqual(entry.AcmeCoField2, deserializedEntry.AcmeCoField2);
            Assert.AreEqual(entry.AcmeCoField3, deserializedEntry.AcmeCoField3);
        }
        public static LogEntry GetEntry(string fileName)
        {
            string serializedContent = ReadFileWithoutLock(fileName);

            serializedContent = SanitizeStringForDeserialization(serializedContent);

            return(BinaryLogFormatter.Deserialize(serializedContent));
        }
        private void ReceiveQueuedMessages()
        {
            this.isCompleted = false;
            while (!IsQueueEmpty())
            {
                using (MessageQueue msmq = CreateMessageQueue())
                {
                    Message message = msmq.Peek();

                    string serializedEntry = message.Body.ToString();

                    LogEntry logEntry = null;
                    try
                    {
                        logEntry = BinaryLogFormatter.Deserialize(serializedEntry);
                    }
                    catch (FormatException formatException)
                    {
                        string logMessage = string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.ExceptionCouldNotDeserializeMessageFromQueue,
                            message.Id,
                            msmq.Path);

                        this.eventLogger.LogServiceFailure(
                            logMessage,
                            formatException,
                            TraceEventType.Error);
                    }
                    catch (SerializationException serializationException)
                    {
                        string logMessage = string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.ExceptionCouldNotDeserializeMessageFromQueue,
                            message.Id,
                            msmq.Path);

                        this.eventLogger.LogServiceFailure(
                            logMessage,
                            serializationException,
                            TraceEventType.Error);
                    }

                    if (logEntry != null)
                    {
                        Logger.Write(logEntry);
                    }

                    message = msmq.Receive();

                    if (this.StopReceiving)
                    {
                        this.isCompleted = true;
                        return;
                    }
                }
            }
        }
        public void CanDeserializeLogEntryXmlUsingBinaryFormatter()
        {
            XmlLogEntry entry = CommonUtil.CreateXmlLogEntry();
            string      serializedLogEntryXmlText = new BinaryLogFormatter().Format(entry);
            XmlLogEntry desiaralizedLogEntryXml   = (XmlLogEntry)BinaryLogFormatter.Deserialize(serializedLogEntryXmlText);

            Assert.IsNotNull(desiaralizedLogEntryXml);
            CommonUtil.AssertXmlLogEntries(entry, desiaralizedLogEntryXml);
        }
Exemplo n.º 7
0
        public void CanCreatePoliciesForBinaryLogFormatter()
        {
            FormatterData data = new BinaryLogFormatterData("name");

            loggingSettings.Formatters.Add(data);
            container.AddExtension(new LoggingBlockExtension());
            BinaryLogFormatter createdObject = (BinaryLogFormatter)container.Resolve <ILogFormatter>("name");

            Assert.IsNotNull(createdObject);
        }
        public void CanCreatePoliciesForBinaryLogFormatter()
        {
            FormatterData data = new BinaryLogFormatterData("name");

            loggingSettings.Formatters.Add(data);

            using (var container = CreateContainer())
            {
                BinaryLogFormatter createdObject = (BinaryLogFormatter)container.Resolve <ILogFormatter>("name");

                Assert.IsNotNull(createdObject);
            }
        }
Exemplo n.º 9
0
        public void WritingMessageSendsSameMessageToMsmq()
        {
            ILogFormatter     formatter = new BinaryLogFormatter();
            MsmqTraceListener listener  =
                new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
                                      MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
                                      false, false, false, MsmqTraceListenerData.DefaultTransactionType, new MockMsmqInterfaceFactory());

            listener.Write("message");

            Message message = MockMsmqInterface.Instance.message;

            Assert.IsNotNull(message);
            Assert.IsNotNull(message.Body);
            Assert.AreEqual(typeof(string), message.Body.GetType());
            Assert.AreEqual("message", message.Body);
        }
Exemplo n.º 10
0
        public void LogToMsmqUsingDirectObjectOnlyResultsInOneMessage()
        {
            ILogFormatter     formatter = new BinaryLogFormatter();
            MsmqTraceListener listener  =
                new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
                                      MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
                                      false, false, false, MsmqTraceListenerData.DefaultTransactionType, new MockMsmqInterfaceFactory());
            TraceSource source = new TraceSource("unnamed", SourceLevels.All);

            source.Listeners.Add(listener);

            int numMessages = MockMsmqInterface.Instance.MessageCount;

            source.TraceData(TraceEventType.Error, 1, new TestCustomObject());
            source.Close();

            int newNumMessages = MockMsmqInterface.Instance.MessageCount;

            Assert.AreEqual(numMessages, newNumMessages - 1);
        }
Exemplo n.º 11
0
        public void CanBuildCorrectMessageWithBinaryLogFormatter()
        {
            ILogFormatter     formatter = new BinaryLogFormatter();
            MsmqTraceListener listener  =
                new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
                                      MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
                                      false, false, false, MessageQueueTransactionType.None);
            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            Message message = listener.CreateMessage(entry);

            Assert.IsNotNull(message);
            Assert.IsNotNull(message.Body);
            Assert.AreEqual(message.Body.GetType(), typeof(string));

            LogEntry deserializedEntry = BinaryLogFormatter.Deserialize(message.Body as string);

            Assert.IsNotNull(deserializedEntry);
            Assert.AreEqual(entry.Message, deserializedEntry.Message);
        }
        public void CanDeserializeFormattedEntry()
        {
            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            entry.Message    = "message";
            entry.Title      = "title";
            entry.Categories = new List <string>(new string[] { "cat1", "cat2", "cat3" });
            string   serializedLogEntryText = new BinaryLogFormatter().Format(entry);
            LogEntry deserializedEntry      = BinaryLogFormatter.Deserialize(serializedLogEntryText);

            Assert.IsNotNull(deserializedEntry);
            Assert.IsFalse(ReferenceEquals(entry, deserializedEntry));
            Assert.AreEqual(entry.Categories.Count, deserializedEntry.Categories.Count);
            foreach (string category in entry.Categories)
            {
                Assert.IsTrue(deserializedEntry.Categories.Contains(category));
            }
            Assert.AreEqual(entry.Message, deserializedEntry.Message);
            Assert.AreEqual(entry.Title, deserializedEntry.Title);
        }
Exemplo n.º 13
0
        public void ShouldNotSendMessageToQueue()
        {
            ILogFormatter     formatter = new BinaryLogFormatter();
            MsmqTraceListener listener  =
                new MsmqTraceListener("unnamed", CommonUtil.MessageQueuePath, formatter, MessagePriority.Low, true,
                                      MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
                                      false, false, false, MsmqTraceListenerData.DefaultTransactionType, new MockMsmqInterfaceFactory());

            // Filter only Critical
            listener.Filter = new EventTypeFilter(SourceLevels.Critical);

            LogSource source = new LogSource("unnamed", new List <TraceListener>(new TraceListener[] { listener }), SourceLevels.All);

            MockMsmqInterface.Instance.transactional = false;
            MockMsmqInterface.Instance.ResetMessageCount();

            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            // Trace an Information LogEntry
            source.TraceData(TraceEventType.Information, 1, entry);

            Assert.AreEqual(0, MockMsmqInterface.Instance.MessageCount);
        }