From 2d41f7aadb4b1e438b5d8f41407f4dd1bf7793b9 Mon Sep 17 00:00:00 2001 From: David Apelt Date: Thu, 11 Jul 2024 14:24:23 +1000 Subject: [PATCH] Add tests for large cell logging in Azure Sink Added two new test methods to the `AzureTableStorageWithPropertiesSinkTests` class to address scenarios involving logging entries that exceed Azure Table Storage's cell size limit using Serilog. The first test, `WhenALoggerWritesToTheSinkAndACellExceeds32KAnExceptionIsRaised`, verifies that an exception is thrown when a log entry exceeds the 32K character limit, noting the test is skipped for the Storage Emulator due to its different limit constraints. The second test, `WhenALoggerWritesToTheSinkAndACellExceeds32KAnExceptionIsRaisedButSubsequentCallsContinueToWorkAsExpected`, checks that after an oversized entry throws an exception, subsequent log entries are processed normally. --- ...zureTableStorageWithPropertiesSinkTests.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/Serilog.Sinks.AzureTableStorage.Tests/AzureTableStorageWithPropertiesSinkTests.cs b/test/Serilog.Sinks.AzureTableStorage.Tests/AzureTableStorageWithPropertiesSinkTests.cs index 342e4b4..986d95f 100644 --- a/test/Serilog.Sinks.AzureTableStorage.Tests/AzureTableStorageWithPropertiesSinkTests.cs +++ b/test/Serilog.Sinks.AzureTableStorage.Tests/AzureTableStorageWithPropertiesSinkTests.cs @@ -571,4 +571,75 @@ public async Task WhenALoggerUsesASASSinkItIsRetrievableFromTheTableWithProperti await table.DeleteAsync(); } + + + + [Fact (Skip = "Does not work with Storage Emulator as it does not feature the 64K cell limit (or 32K string)")] + public async Task WhenALoggerWritesToTheSinkAndACellExceeds32KAnExceptionIsRaised() + { + var storageAccount = new TableServiceClient(DevelopmentStorageAccountConnectionString); + var table = storageAccount.GetTableClient($"LogUnitTest{DateTime.Now.Ticks}"); + + var logger = new LoggerConfiguration() + .WriteTo.AzureTableStorage( + storageAccount: storageAccount, + storageTableName: table.Name) + .CreateLogger(); + + var exception = new ArgumentException("Some exception"); + + const string messageTemplate = "{tooBig}"; + + var tooBig = new string('a', 33000); + + Assert.Throws(() => logger.Information(exception, messageTemplate, tooBig)); + + logger.Dispose(); + + await table.DeleteAsync(); + } + + [Fact (Skip = "Does not work with Storage Emulator as it does not feature the 64K cell limit (or 32K string)")] + public async Task WhenALoggerWritesToTheSinkAndACellExceeds32KAnExceptionIsRaisedButSubsequentCallsContinueToWorkAsExpected() + { + var storageAccount = new TableServiceClient(DevelopmentStorageAccountConnectionString); + var table = storageAccount.GetTableClient($"LogUnitTest{DateTime.Now.Ticks}"); + + var logger = new LoggerConfiguration() + .WriteTo.AzureTableStorage( + storageAccount: storageAccount, + storageTableName: table.Name) + .CreateLogger(); + + var exception = new ArgumentException("Some exception"); + + const string messageTemplate = "{tooBig}"; + + var tooBig = new string('a', 33000); + var notTooBig = new string('b', 1000); + + Assert.Throws(() => logger.Information(exception, messageTemplate, tooBig)); + + // After the expected exception, the logger should continue to work on subsequent calls + const string messageTemplate2 = "{Properties} should go in their {Numbered} {Space}"; + + logger.Information(exception, messageTemplate2, "Properties", 1234, ' '); + logger.Dispose(); + + var result = (await TableQueryTakeDynamicAsync(table, takeCount: 1)).First(); + + // Check the presence of same properties as in previous version + Assert.Equal(messageTemplate2, result["MessageTemplate"]); + Assert.Equal("Information", result["Level"]); + Assert.Equal("System.ArgumentException: Some exception", result["Exception"]); + Assert.Equal("\"Properties\" should go in their 1234 ", result["RenderedMessage"]); + + // Check the presence of the new properties. + Assert.Equal("Properties", result["Properties"]); + Assert.Equal(1234, result["Numbered"]); + Assert.Equal(" ", result["Space"]); + + await table.DeleteAsync(); + } + }