Beispiel #1
0
        public void Test_EventHubLog_MessageCancellation_UnableToCancel()
        {
            var azureStorageLogger = new AzureStorageEventLogger(blobContainerClient);

            var result = azureStorageLogger.CancelLogFileStoringOperation();

            Assert.AreEqual(AzureStorageEventLogCancellationResult.UnableToCancel, result);
        }
Beispiel #2
0
        public void Test_GenerateErrorBlobName_WithGuidOnly()
        {
            var azureStorageLogger = new AzureStorageEventLogger(blobContainerClient);
            var name     = Guid.NewGuid();
            var result   = azureStorageLogger.GenerateErrorBlobName(name);
            var expected = string.Format("{0}.{1}", name.ToString().Replace("-", "_"), "json");

            Assert.AreEqual(expected, result);
        }
Beispiel #3
0
        public void Test_GeneratePathForErrorBlob()
        {
            var azureStorageLogger = new AzureStorageEventLogger(blobContainerClient);
            var dt = new DateTime(2021, 12, 10, 12, 13, 14);
            //DateTime.Parse("2020-12-10 12:13:14");
            var result   = azureStorageLogger.GeneratePathForErrorBlob(dt);
            var expected = "2021\\12\\10\\12\\13\\14";

            Assert.AreEqual(expected, result);
        }
Beispiel #4
0
        public void Test_GenerateServiceName()
        {
            var azureStorageLogger = new AzureStorageEventLogger(blobContainerClient);
            var serviceName        = "testService";
            var environment        = "testEnvironment";
            var result             = azureStorageLogger.GenerateServiceName(serviceName, environment);
            var expected           = string.Format("{0} - {1}", serviceName, environment);

            Assert.AreEqual(expected, result);
        }
Beispiel #5
0
        public void Test_EventHubLog_MessageCancellation_CancelSuccessfully()
        {
            var azureStorageLogger = new AzureStorageEventLogger(blobContainerClient);
            var azureStorageModel  = new AzureStorageEventModel("test service - test environment/day/test.json", GenerateTestMessage(512 * 512));

            azureStorageLogger.StoreLogFile(azureStorageModel, true);
            var result = azureStorageLogger.CancelLogFileStoringOperation();

            Assert.AreEqual(AzureStorageEventLogCancellationResult.Successful, result);
        }
Beispiel #6
0
        public async void Log(LogEntry logEntry)
        {
            try
            {
                string jsonLogEntry;
                try
                {
                    jsonLogEntry = JsonConvert.SerializeObject(logEntry, settings);
                }
                catch (Exception e)
                {
                    logEntry = new LogEntry()
                    {
                        Exception       = e,
                        Level           = "Warning",
                        MessageTemplate = "Log Serialization failed with exception",
                        Timestamp       = DateTime.UtcNow,
                        EventId         = new EventId(7437)
                    };
                    jsonLogEntry = JsonConvert.SerializeObject(logEntry, settings);
                }

                AzureStorageLoggingCheckResult azureStorageLoggingCheckResult = this.azureStorageBlobContainerBuilder.NeedsAzureStorageLogging(jsonLogEntry, 1);

                switch (azureStorageLoggingCheckResult)
                {
                case AzureStorageLoggingCheckResult.LogWarningNoStorage:
                    jsonLogEntry = logEntry.ToLongMessageWarning(settings);
                    break;

                case AzureStorageLoggingCheckResult.LogWarningAndStoreMessage:
                    var    azureLogger  = new AzureStorageEventLogger(this.azureStorageBlobContainerBuilder.BlobContainerClient);
                    string blobFullName = azureLogger.GenerateBlobFullName(
                        new AzureStorageBlobFullNameModel(azureLogger.GenerateServiceName(
                                                              logEntry.LogProperties.GetLogEntryPropertyValue("_Service"),
                                                              logEntry.LogProperties.GetLogEntryPropertyValue("_Environment")),
                                                          azureLogger.GeneratePathForErrorBlob(logEntry.Timestamp),
                                                          azureLogger.GenerateErrorBlobName()));
                    var azureStorageModel = new AzureStorageEventModel(blobFullName, jsonLogEntry);
                    var result            = await azureLogger.StoreLogFileAsync(azureStorageModel);

                    jsonLogEntry = result.ToJsonLogEntryString(this.azureStorageBlobContainerBuilder.AzureStorageLogProviderOptions, logEntry, settings);
                    break;
                }

                await eventHubClientWrapper.SendAsync(new EventData(Encoding.UTF8.GetBytes(jsonLogEntry)));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Beispiel #7
0
        public void Test_GenerateBlobFullName()
        {
            var azureStorageLogger = new AzureStorageEventLogger(blobContainerClient);
            var name                  = Guid.NewGuid();
            var extension             = "json";
            var serviceName           = "testService";
            var path                  = "2021\\12\\10\\12\\13\\14";
            var blobName              = string.Format("{0}.{1}", name.ToString().Replace("-", "_"), extension);
            var azureStorageBlobModel = new AzureStorageBlobFullNameModel(serviceName, path, blobName);
            var result                = azureStorageLogger.GenerateBlobFullName(azureStorageBlobModel);
            var expected              = Path.Combine(serviceName, path, blobName);

            Assert.AreEqual(expected, result);
        }
Beispiel #8
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);
        }
Beispiel #9
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:"));
        }