public void MessageIsDeserializedWhenUsingJsonFormatterWithMsmqTraceListener()
        {
            MsmqTraceListener listener =
                new MsmqTraceListener("unnamed", MsmqUtil.MessageQueuePath, new JsonLogFormatter(), MessagePriority.Low, true,
                                      MsmqTraceListenerData.DefaultTimeToBeReceived, MsmqTraceListenerData.DefaultTimeToReachQueue,
                                      false, false, false, MessageQueueTransactionType.None);
            LogEntry entry = MsmqUtil.GetDefaultLogEntry();

            Message message = listener.CreateMessage(entry);

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

            LogEntry deserializedEntry = JsonLogFormatter.Deserialize <LogEntry>(message.Body as string);

            Assert.IsNotNull(deserializedEntry);
            Assert.AreEqual(entry.Message, deserializedEntry.Message);
        }
Esempio n. 2
0
        public void EntryIsWrittenWhenMSMQGeneralCategory()
        {
            LoggingConfiguration loggingConfiguration = BuildProgrammaticConfigForTrace();
            var msmqListener = new MsmqTraceListener("TestMSMQProg", MsmqUtil.MessageQueuePath, new BinaryLogFormatter(),
                                                     MessagePriority.Normal, false, new TimeSpan(0, 1, 0), new TimeSpan(0, 1, 0),
                                                     false, true, false, MessageQueueTransactionType.None);

            msmqListener.Filter = new EventTypeFilter(SourceLevels.All);

            MsmqUtil.ValidateMsmqIsRunning();
            MsmqUtil.DeletePrivateTestQ();
            MsmqUtil.CreatePrivateTestQ();

            LogSource clientSource      = new LogSource("TestMSMQProg", new[] { msmqListener }, SourceLevels.All);
            LogSource distributorSource = new LogSource("TestMSMQProg", new[] { new MockTraceListener() }, SourceLevels.All);

            Dictionary <string, LogSource> traceSources = new Dictionary <string, LogSource>();

            this.writer = new LogWriter(new List <ILogFilter>(), traceSources, distributorSource, null, new LogSource("errors"), "General", false, false);
            Logger.SetLogWriter(this.writer);

            DistributorEventLogger eventLogger     = new DistributorEventLogger();
            MsmqLogDistributor     msmqDistributor = new MsmqLogDistributor(MsmqUtil.MessageQueuePath, eventLogger);

            msmqDistributor.StopReceiving = false;

            LogEntry logEntry = MsmqUtil.GetDefaultLogEntry();

            logEntry.Categories = new string[] { "MockCategoryOne" };
            logEntry.Message    = MsmqUtil.MsgBody;
            logEntry.Severity   = TraceEventType.Information;

            clientSource.TraceData(logEntry.Severity, 1, logEntry);

            msmqDistributor.CheckForMessages();

            Assert.AreEqual(1, MockTraceListener.Entries.Count);
            Assert.AreEqual(MsmqUtil.MsgBody, MockTraceListener.LastEntry.Message, "Body");
        }
        public void EntryIsWrittenWithNoFormattingWhenUsingJsonFormatterWithFormattedEventLogTraceListener()
        {
            var eventLog = new EventLog(LoggingFixtureBase.EventLogName, ".", "Enterprise Library Logging");

            FormattedEventLogTraceListener listener =
                new FormattedEventLogTraceListener(eventLog, new JsonLogFormatter());

            LoggingConfiguration loggingConfiguration = BuildProgrammaticConfigForTrace();

            loggingConfiguration.AddLogSource("notfromconfig", SourceLevels.All, true, listener);

            LogEntry logEntry = MsmqUtil.GetDefaultLogEntry();

            logEntry.Categories = new string[] { "notfromconfig" };

            this.writer = new LogWriter(loggingConfiguration);
            this.writer.Write(logEntry);

            EventLogEntry eventLogEntry = GetLastEventLogEntry();

            Assert.IsTrue(eventLogEntry.Message.IndexOf("\"Message\":\"My message body\",\"Categories\":[\"notfromconfig\"],\"Priority\":100,\"EventId\":1,\"Severity\":8,\"LoggedSeverity\":\"Information\",\"Title\":\"=== Header ===\",\"TimeStamp\":") != -1);
        }