public void TestSerializesJsonCorrctlyForCorrectLogEntry()
        {
            var fakeEventHubClient = A.Fake <IEventHubClientWrapper>();

            byte[] sentBytes = null;
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).Invokes((EventData ed) => sentBytes = ed.Body.Array);

            var eventHubLog  = new EventHubLog(fakeEventHubClient);
            var testLogEntry = new LogEntry()
            {
                EventId       = new EventId(2),
                Timestamp     = new DateTime(2002, 03, 04),
                Exception     = new InvalidOperationException("TestLoggedException"),
                LogProperties = new Dictionary <string, object> {
                    { "hi", "Guys" }
                },
                MessageTemplate = "Hello this is a message template",
                Level           = "Debug"
            };

            eventHubLog.Log(testLogEntry);
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).MustHaveHappenedOnceExactly();

            var sentString   = Encoding.UTF8.GetString(sentBytes);
            var sentLogEntry = JsonConvert.DeserializeObject <LogEntry>(sentString);

            Assert.AreEqual(testLogEntry.Timestamp, sentLogEntry.Timestamp);
            Assert.AreEqual(testLogEntry.MessageTemplate, sentLogEntry.MessageTemplate);
            CollectionAssert.AreEqual(testLogEntry.LogProperties, sentLogEntry.LogProperties);
            Assert.AreEqual(testLogEntry.EventId, sentLogEntry.EventId);
            Assert.AreEqual(testLogEntry.Exception.Message, sentLogEntry.Exception.Message);
            Assert.AreEqual(testLogEntry.Level, sentLogEntry.Level);
        }
Example #2
0
        public void Test_EventHubLog_ForMessagesLessThan1MB()
        {
            var fakeEventHubClient = A.Fake <IEventHubClientWrapper>();
            var service            = "test service";
            var environment        = "test environment";
            var testLogProperties  = new Dictionary <string, object> {
                { "_Service", service }, { "_Environment", environment }
            };
            var testDateStamp = new DateTime(2002, 03, 04);

            byte[] sentBytes = null;
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).Invokes((EventData ed) => sentBytes = ed.Body.Array);
            var template     = GenerateTestMessage(1024 * 512);
            var eventHubLog  = new EventHubLog(fakeEventHubClient);
            var testLogEntry = new LogEntry
            {
                EventId         = new EventId(2),
                Timestamp       = testDateStamp,
                Exception       = new InvalidOperationException("TestLoggedException"),
                LogProperties   = testLogProperties,
                MessageTemplate = template,
                Level           = "LogLevel"
            };

            eventHubLog.Log(testLogEntry);
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).MustHaveHappenedOnceExactly();

            var sentString   = Encoding.UTF8.GetString(sentBytes);
            var sentLogEntry = JsonConvert.DeserializeObject <LogEntry>(sentString);

            Assert.AreEqual(testLogEntry.Timestamp, sentLogEntry.Timestamp);
            CollectionAssert.AreEqual(testLogEntry.LogProperties, sentLogEntry.LogProperties);
            Assert.AreEqual(testLogEntry.EventId, sentLogEntry.EventId);
            Assert.AreEqual(testLogEntry.Level, sentLogEntry.Level);
            Assert.AreEqual(2, sentLogEntry.LogProperties.Count);
            Assert.AreEqual(testLogEntry.LogProperties.First(), sentLogEntry.LogProperties.First());

            Assert.AreEqual(testLogEntry.EventId, sentLogEntry.EventId);
            Assert.AreEqual(testLogEntry.Level, sentLogEntry.Level);

            var azureLogger = new AzureStorageEventLogger(fakeEventHubClient.AzureStorageBlobContainerBuilder.BlobContainerClient);

            Assert.AreEqual(sentLogEntry.MessageTemplate, template);
            Assert.AreEqual(testLogEntry.Exception.Message, sentLogEntry.Exception.Message);
        }
Example #3
0
        public void Test_EventHubLog_ForMessagesEqualTo1MB()
        {
            var fakeEventHubClient = A.Fake <IEventHubClientWrapper>();
            var service            = "test service";
            var environment        = "test environment";
            var testLogProperties  = new Dictionary <string, object> {
                { "_Service", service }, { "_Environment", environment }
            };
            var testDateStamp = new DateTime(2002, 03, 04);

            byte[] sentBytes = null;
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).Invokes((EventData ed) => sentBytes = ed.Body.Array);
            string template     = null;
            var    eventHubLog  = new EventHubLog(fakeEventHubClient);
            var    testLogEntry = new LogEntry
            {
                EventId         = new EventId(2),
                Timestamp       = testDateStamp,
                Exception       = new InvalidOperationException("TestLoggedException"),
                LogProperties   = testLogProperties,
                MessageTemplate = string.Empty,                    //find the size of the rest of the object so that we can create it exactly 1 mb
                Level           = "LogLevel"
            };

            template = CreateMessageEqualTo1Mb(testLogEntry);
            testLogEntry.MessageTemplate = template;
            eventHubLog.Log(testLogEntry);
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).MustHaveHappenedOnceExactly();

            var sentString   = Encoding.UTF8.GetString(sentBytes);
            var sentLogEntry = JsonConvert.DeserializeObject <LogEntry>(sentString);

            Assert.AreEqual(testLogEntry.Timestamp, sentLogEntry.Timestamp);
            CollectionAssert.AreEqual(testLogEntry.LogProperties, sentLogEntry.LogProperties);
            Assert.AreEqual(testLogEntry.EventId, sentLogEntry.EventId);
            Assert.AreEqual(testLogEntry.Level, sentLogEntry.Level);

            Assert.AreEqual(testLogEntry.EventId, sentLogEntry.EventId);
            Assert.AreEqual(testLogEntry.Level, sentLogEntry.Level);

            var azureLogger = new AzureStorageEventLogger(fakeEventHubClient.AzureStorageBlobContainerBuilder.BlobContainerClient);

            Assert.IsTrue(sentLogEntry.MessageTemplate.StartsWith("Azure Storage Logging:"));
            Assert.IsTrue(sentLogEntry.Exception.Message.StartsWith("Azure Storage Logging:"));
        }
        public void TestSerializesJsonWhenPropertyThrowsException()
        {
            var fakeEventHubClient = A.Fake <IEventHubClientWrapper>();

            byte[] sentBytes = null;
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).Invokes((EventData ed) => sentBytes = ed.Body.Array);

            var eventHubLog  = new EventHubLog(fakeEventHubClient);
            var testLogEntry = new LogEntry()
            {
                EventId       = new EventId(2),
                Timestamp     = new DateTime(2002, 03, 04),
                Exception     = new InvalidOperationException("TestLoggedException"),
                LogProperties = new Dictionary <string, object> {
                    { "hi", "Guys" }, { "thowable", new ObjectThatThrows() }
                },
                MessageTemplate = "Hello this is a message template",
                Level           = "LogLevel"
            };

            eventHubLog.Log(testLogEntry);
            A.CallTo(() => fakeEventHubClient.SendAsync(A <EventData> .Ignored)).MustHaveHappenedOnceExactly();

            var sentString   = Encoding.UTF8.GetString(sentBytes);
            var sentLogEntry = JsonConvert.DeserializeObject <LogEntry>(sentString);

            Assert.AreEqual(testLogEntry.Timestamp, sentLogEntry.Timestamp);
            Assert.AreEqual(testLogEntry.MessageTemplate, sentLogEntry.MessageTemplate);

            Assert.AreEqual(2, sentLogEntry.LogProperties.Count);
            Assert.AreEqual(testLogEntry.LogProperties.First(), sentLogEntry.LogProperties.First());

            var asJObject = (JObject)sentLogEntry.LogProperties.Skip(1).First().Value;

            Assert.AreEqual(new ObjectThatThrows().NotThrowable, asJObject["NotThrowable"].Value <string>());

            Assert.AreEqual(testLogEntry.EventId, sentLogEntry.EventId);
            Assert.AreEqual(testLogEntry.Exception.Message, sentLogEntry.Exception.Message);
            Assert.AreEqual(testLogEntry.Level, sentLogEntry.Level);
        }