diff --git a/test/AWS.Logger.AspNetCore.Tests/TestClass.cs b/test/AWS.Logger.AspNetCore.Tests/TestClass.cs index 727f863..6254591 100644 --- a/test/AWS.Logger.AspNetCore.Tests/TestClass.cs +++ b/test/AWS.Logger.AspNetCore.Tests/TestClass.cs @@ -80,10 +80,10 @@ private void LoggingSetup(string configFileName, string configSectionInfoBlockNa /// 10 debug messages to CloudWatchLogs. The results are then verified. /// [Fact] - public void ILogger() + public async Task ILogger() { LoggingSetup("appsettings.json",null); - SimpleLoggingTest(ConfigSection.Config.LogGroup); + await SimpleLoggingTest(ConfigSection.Config.LogGroup); } [Fact] @@ -129,10 +129,10 @@ public async Task MultiThreadTestMock() /// threads that log 200 debug messages each to CloudWatchLogs. The results are then verified. /// [Fact] - public void MultiThreadTest() + public async Task MultiThreadTest() { LoggingSetup("multiThreadTest.json",null); - MultiThreadTestGroup(ConfigSection.Config.LogGroup); + await MultiThreadTestGroup(ConfigSection.Config.LogGroup); } /// @@ -143,17 +143,17 @@ public void MultiThreadTest() /// inorder to force a buffer full scenario. The results are then verified. /// [Fact] - public void MultiThreadBufferFullTest() + public async Task MultiThreadBufferFullTest() { LoggingSetup("multiThreadBufferFullTest.json",null); - MultiThreadBufferFullTestGroup(ConfigSection.Config.LogGroup); + await MultiThreadBufferFullTestGroup(ConfigSection.Config.LogGroup); } [Fact] - public void OverrideLogStreamNameTest() + public async Task OverrideLogStreamNameTest() { LoggingSetup("overrideLogStreamName.json", null); - MultiThreadTestGroup(ConfigSection.Config.LogGroup, ConfigSection.Config.LogStreamName); + await MultiThreadTestGroup(ConfigSection.Config.LogGroup, ConfigSection.Config.LogStreamName); } /// @@ -165,7 +165,7 @@ protected override void LogMessages(int count) Logger.LogError(0, new Exception("Exception message."), "Error message"); for (int i = 0; i < count-2; i++) { - Logger.LogDebug(string.Format("Test logging message {0} Ilogger, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId)); + Logger.LogDebug(string.Format("Test logging message {0} Ilogger, Thread Id:{1}", i, Environment.CurrentManagedThreadId)); } Logger.LogDebug(LASTMESSAGE); } diff --git a/test/AWS.Logger.Log4Net.Tests/MultiThreadBufferFullTest.config b/test/AWS.Logger.Log4Net.Tests/MultiThreadBufferFullTest.config index d1ffa91..ef677b2 100644 --- a/test/AWS.Logger.Log4Net.Tests/MultiThreadBufferFullTest.config +++ b/test/AWS.Logger.Log4Net.Tests/MultiThreadBufferFullTest.config @@ -3,7 +3,7 @@ - AWSLog4NetGroupMultiThreadBufferFullTest + {LOG_GROUP_NAME} us-west-2 10 diff --git a/test/AWS.Logger.Log4Net.Tests/MultiThreadTest.config b/test/AWS.Logger.Log4Net.Tests/MultiThreadTest.config index b69d9ae..ed251b3 100644 --- a/test/AWS.Logger.Log4Net.Tests/MultiThreadTest.config +++ b/test/AWS.Logger.Log4Net.Tests/MultiThreadTest.config @@ -3,7 +3,7 @@ - AWSLog4NetGroupLog4NetMultiThreadTest + {LOG_GROUP_NAME} us-west-2 diff --git a/test/AWS.Logger.Log4Net.Tests/OverrideLogStreamName.config b/test/AWS.Logger.Log4Net.Tests/OverrideLogStreamName.config index 967b436..f188288 100644 --- a/test/AWS.Logger.Log4Net.Tests/OverrideLogStreamName.config +++ b/test/AWS.Logger.Log4Net.Tests/OverrideLogStreamName.config @@ -4,7 +4,7 @@ - AWSLog4NetGroupOverrideLogStreamName + {LOG_GROUP_NAME} CustomStreamName us-west-2 Custom diff --git a/test/AWS.Logger.Log4Net.Tests/Properties/AssemblyInfo.cs b/test/AWS.Logger.Log4Net.Tests/Properties/AssemblyInfo.cs index 9fd7691..4a952b8 100644 --- a/test/AWS.Logger.Log4Net.Tests/Properties/AssemblyInfo.cs +++ b/test/AWS.Logger.Log4Net.Tests/Properties/AssemblyInfo.cs @@ -29,3 +29,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] + +[assembly: Xunit.CollectionBehavior(DisableTestParallelization = true)] \ No newline at end of file diff --git a/test/AWS.Logger.Log4Net.Tests/TestClass.cs b/test/AWS.Logger.Log4Net.Tests/TestClass.cs index 4c54366..f18d013 100644 --- a/test/AWS.Logger.Log4Net.Tests/TestClass.cs +++ b/test/AWS.Logger.Log4Net.Tests/TestClass.cs @@ -1,6 +1,11 @@ using System; +using System.IO; +using System.Linq; using System.Reflection; +using System.Text; using System.Threading; +using System.Threading.Tasks; +using AWS.Logger.Log4net; using AWS.Logger.TestUtils; using log4net; using log4net.Config; @@ -12,13 +17,18 @@ public class Log4NetTestClass : BaseTestClass { public ILog Logger; - private void GetLog4NetLogger(string fileName, string logName) + private void GetLog4NetLogger(string fileName, string logName, string logGroupName) { // Create logger var repositoryAssembly = typeof(Log4NetTestClass).GetTypeInfo().Assembly; var loggerRepository = LogManager.GetRepository(repositoryAssembly); - XmlConfigurator.Configure(loggerRepository, new System.IO.FileInfo(fileName)); - Logger = LogManager.GetLogger(repositoryAssembly, logName); + var fileInfo = new FileInfo(fileName); + var fileContent = File.ReadAllText(fileInfo.FullName); + using (Stream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent.Replace("{LOG_GROUP_NAME}", logGroupName)))) + { + XmlConfigurator.Configure(loggerRepository, memoryStream); + Logger = LogManager.GetLogger(repositoryAssembly, logName); + } } public Log4NetTestClass(TestFixture testFixture) : base(testFixture) @@ -27,24 +37,27 @@ public Log4NetTestClass(TestFixture testFixture) : base(testFixture) #region Test Cases [Fact] - public void Log4Net() + public async Task Log4Net() { - GetLog4NetLogger("log4net.config","Log4Net"); - SimpleLoggingTest("AWSLog4NetGroupLog4Net"); + var logGroupName = $"AWSLog4NetGroupLog4Net{Guid.NewGuid().ToString().Split('-').Last()}"; + GetLog4NetLogger("log4net.config","Log4Net", logGroupName); + await SimpleLoggingTest(logGroupName); } [Fact] - public void MultiThreadTest() + public async Task MultiThreadTest() { - GetLog4NetLogger("MultiThreadTest.config", "MultiThreadTest"); - MultiThreadTestGroup("AWSLog4NetGroupLog4NetMultiThreadTest"); + var logGroupName = $"AWSLog4NetGroupLog4NetMultiThreadTest{Guid.NewGuid().ToString().Split('-').Last()}"; + GetLog4NetLogger("MultiThreadTest.config", "MultiThreadTest", logGroupName); + await MultiThreadTestGroup(logGroupName); } [Fact] - public void MultiThreadBufferFullTest() + public async Task MultiThreadBufferFullTest() { - GetLog4NetLogger("MultiThreadBufferFullTest.config", "MultiThreadBufferFullTest"); - MultiThreadBufferFullTestGroup("AWSLog4NetGroupMultiThreadBufferFullTest"); + var logGroupName = $"AWSLog4NetGroupMultiThreadBufferFullTest{Guid.NewGuid().ToString().Split('-').Last()}"; + GetLog4NetLogger("MultiThreadBufferFullTest.config", "MultiThreadBufferFullTest", logGroupName); + await MultiThreadBufferFullTestGroup(logGroupName); } /// @@ -52,17 +65,18 @@ public void MultiThreadBufferFullTest() /// when an override log stream name is provided /// [Fact] - public void CustomLogStreamNameTest() + public async Task CustomLogStreamNameTest() { - GetLog4NetLogger("OverrideLogStreamName.config", "OverrideLogStreamName"); - MultiThreadTestGroup("AWSLog4NetGroupOverrideLogStreamName"); + var logGroupName = $"AWSLog4NetGroupMultiThreadBufferFullTest{Guid.NewGuid().ToString().Split('-').Last()}"; + GetLog4NetLogger("OverrideLogStreamName.config", "OverrideLogStreamName", logGroupName); + await MultiThreadTestGroup(logGroupName); } protected override void LogMessages(int count) { for (int i = 0; i < count-1; i++) { - Logger.Debug(string.Format("Test logging message {0} Log4Net, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId)); + Logger.Debug(string.Format("Test logging message {0} Log4Net, Thread Id:{1}", i, Environment.CurrentManagedThreadId)); } Logger.Debug(LASTMESSAGE); } diff --git a/test/AWS.Logger.Log4Net.Tests/log4net.config b/test/AWS.Logger.Log4Net.Tests/log4net.config index 18456a0..265f88c 100644 --- a/test/AWS.Logger.Log4Net.Tests/log4net.config +++ b/test/AWS.Logger.Log4Net.Tests/log4net.config @@ -4,7 +4,7 @@ - AWSLog4NetGroupLog4Net + {LOG_GROUP_NAME} us-west-2 Custom CustomPrefix diff --git a/test/AWS.Logger.NLog.Tests/AWSNLogGroupEventSizeExceededTest.config b/test/AWS.Logger.NLog.Tests/AWSNLogGroupEventSizeExceededTest.config index 9f1124f..e202b72 100644 --- a/test/AWS.Logger.NLog.Tests/AWSNLogGroupEventSizeExceededTest.config +++ b/test/AWS.Logger.NLog.Tests/AWSNLogGroupEventSizeExceededTest.config @@ -5,10 +5,10 @@ - + - + \ No newline at end of file diff --git a/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadBufferFullTest.config b/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadBufferFullTest.config index fcb503f..83cc079 100644 --- a/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadBufferFullTest.config +++ b/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadBufferFullTest.config @@ -5,10 +5,10 @@ - + - + \ No newline at end of file diff --git a/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadTest.config b/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadTest.config index a2ddac2..46ebb69 100644 --- a/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadTest.config +++ b/test/AWS.Logger.NLog.Tests/AWSNLogGroupMultiThreadTest.config @@ -5,10 +5,10 @@ - + - + \ No newline at end of file diff --git a/test/AWS.Logger.NLog.Tests/AWSNLogOverrideLogStreamName.config b/test/AWS.Logger.NLog.Tests/AWSNLogOverrideLogStreamName.config index bbac65b..dfff0bd 100644 --- a/test/AWS.Logger.NLog.Tests/AWSNLogOverrideLogStreamName.config +++ b/test/AWS.Logger.NLog.Tests/AWSNLogOverrideLogStreamName.config @@ -5,10 +5,10 @@ - + - + \ No newline at end of file diff --git a/test/AWS.Logger.NLog.Tests/Regular.config b/test/AWS.Logger.NLog.Tests/Regular.config index 1312b8a..33cd126 100644 --- a/test/AWS.Logger.NLog.Tests/Regular.config +++ b/test/AWS.Logger.NLog.Tests/Regular.config @@ -5,10 +5,10 @@ - + - + \ No newline at end of file diff --git a/test/AWS.Logger.NLog.Tests/TestClass.cs b/test/AWS.Logger.NLog.Tests/TestClass.cs index 742f8fc..c7032a6 100644 --- a/test/AWS.Logger.NLog.Tests/TestClass.cs +++ b/test/AWS.Logger.NLog.Tests/TestClass.cs @@ -1,6 +1,10 @@ using System; +using System.IO; +using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Xml; using Amazon.CloudWatchLogs.Model; using AWS.Logger.TestUtils; using NLog; @@ -15,9 +19,15 @@ public class NLogTestClass: BaseTestClass { public NLog.Logger Logger; - private void CreateLoggerFromConfiguration(string configFileName) + private void CreateLoggerFromConfiguration(string configFileName, string logGroupName) { - LogManager.Configuration = new XmlLoggingConfiguration(configFileName); + var fileInfo = new FileInfo(configFileName); + var fileContent = File.ReadAllText(fileInfo.FullName); + using (Stream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent.Replace("{LOG_GROUP_NAME}", logGroupName)))) + using (XmlReader reader = XmlReader.Create(memoryStream)) + { + LogManager.Configuration = new XmlLoggingConfiguration(reader, configFileName); + } } public NLogTestClass(TestFixture testFixture) : base(testFixture) @@ -26,27 +36,30 @@ public NLogTestClass(TestFixture testFixture) : base(testFixture) #region Test Cases [Fact] - public void Nlog() + public async Task Nlog() { - CreateLoggerFromConfiguration("Regular.config"); + var logGroupName = $"AWSNLogGroup{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("Regular.config", logGroupName); Logger = LogManager.GetLogger("loggerRegular"); - SimpleLoggingTest("AWSNLogGroup"); + await SimpleLoggingTest(logGroupName); } [Fact] - public void MultiThreadTest() + public async Task MultiThreadTest() { - CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadTest.config"); + var logGroupName = $"AWSNLogGroupMultiThreadTest{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadTest.config", logGroupName); Logger = LogManager.GetLogger("loggerMultiThread"); - MultiThreadTestGroup("AWSNLogGroupMultiThreadTest"); + await MultiThreadTestGroup(logGroupName); } [Fact] - public void MultiThreadBufferFullTest() + public async Task MultiThreadBufferFullTest() { - CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadBufferFullTest.config"); + var logGroupName = $"AWSNLogGroupMultiThreadBufferFullTest{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("AWSNLogGroupMultiThreadBufferFullTest.config", logGroupName); Logger = LogManager.GetLogger("loggerMultiThreadBufferFull"); - MultiThreadBufferFullTestGroup("AWSNLogGroupMultiThreadBufferFullTest"); + await MultiThreadBufferFullTestGroup(logGroupName); } /// @@ -54,19 +67,20 @@ public void MultiThreadBufferFullTest() /// when an override log stream name is provided /// [Fact] - public void CustomLogStreamNameTest() + public async Task CustomLogStreamNameTest() { - CreateLoggerFromConfiguration("AWSNLogOverrideLogStreamName.config"); + var logGroupName = $"AWSNLogOverrideLogStreamName{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("AWSNLogOverrideLogStreamName.config", logGroupName); Logger = LogManager.GetLogger("overrideLogStreamName"); - MultiThreadTestGroup("AWSNLogOverrideLogStreamName", "CustomStreamName"); + await MultiThreadTestGroup(logGroupName, "CustomStreamName"); } [Fact] public async Task MessageHasToBeBrokenUp() { - string logGroupName = "AWSNLogGroupEventSizeExceededTest"; + var logGroupName = $"AWSNLogGroupEventSizeExceededTest{Guid.NewGuid().ToString().Split('-').Last()}"; - CreateLoggerFromConfiguration("AWSNLogGroupEventSizeExceededTest.config"); + CreateLoggerFromConfiguration("AWSNLogGroupEventSizeExceededTest.config", logGroupName); Logger = LogManager.GetLogger("loggerRegularEventSizeExceeded"); // This will get broken up into 3 CloudWatch Log messages @@ -74,10 +88,10 @@ public async Task MessageHasToBeBrokenUp() Logger.Debug(LASTMESSAGE); GetLogEventsResponse getLogEventsResponse = new GetLogEventsResponse(); - if (NotifyLoggingCompleted(logGroupName, "LASTMESSAGE")) + if (await NotifyLoggingCompleted(logGroupName, "LASTMESSAGE")) { DescribeLogStreamsResponse describeLogstreamsResponse = - await Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest + await _testFixture.Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest { Descending = true, LogGroupName = logGroupName, @@ -86,7 +100,7 @@ await Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest // Wait for the large messages to propagate Thread.Sleep(5000); - getLogEventsResponse = await Client.GetLogEventsAsync(new GetLogEventsRequest + getLogEventsResponse = await _testFixture.Client.GetLogEventsAsync(new GetLogEventsRequest { LogGroupName = logGroupName, LogStreamName = describeLogstreamsResponse.LogStreams[0].LogStreamName @@ -100,7 +114,7 @@ protected override void LogMessages(int count) { for (int i = 0; i < count-1; i++) { - Logger.Debug(string.Format("Test logging message {0} NLog, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId)); + Logger.Debug(string.Format("Test logging message {0} NLog, Thread Id:{1}", i, Environment.CurrentManagedThreadId)); } Logger.Debug(LASTMESSAGE); } diff --git a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroup.json b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroup.json index 956c3c4..e2b16b7 100644 --- a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroup.json +++ b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroup.json @@ -2,7 +2,7 @@ "Serilog": { "Using": [ "AWS.Logger.SeriLog" ], "MinimumLevel": "Debug", - "LogGroup": "AWSSeriLogGroup", + "LogGroup": "{LOG_GROUP_NAME}", "Region": "us-west-2", "LogStreamNameSuffix": "Custom", "LogStreamNamePrefix": "CustomPrefix", diff --git a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadBufferFullTest.json b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadBufferFullTest.json index dcf0cd9..be82619 100644 --- a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadBufferFullTest.json +++ b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadBufferFullTest.json @@ -2,7 +2,7 @@ "Serilog": { "Using": [ "AWS.Logger.SeriLog" ], "MinimumLevel": "Debug", - "LogGroup": "AWSSeriLogGroupMultiThreadBufferFullTest", + "LogGroup": "{LOG_GROUP_NAME}", "Region": "us-west-2", "LogStreamNameSuffix": "Custom", "MaxQueuedMessages": "100", diff --git a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadTest.json b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadTest.json index bef5540..e6a3dfb 100644 --- a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadTest.json +++ b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupMultiThreadTest.json @@ -2,7 +2,7 @@ "Serilog": { "Using": [ "AWS.Logger.SeriLog" ], "MinimumLevel": "Debug", - "LogGroup": "AWSSeriLogGroupMultiThreadTest", + "LogGroup": "{LOG_GROUP_NAME}", "Region": "us-west-2", "LogStreamNameSuffix": "Custom", "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ] diff --git a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupOverrideLogStreamName.json b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupOverrideLogStreamName.json index db09be4..bed2453 100644 --- a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupOverrideLogStreamName.json +++ b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupOverrideLogStreamName.json @@ -2,7 +2,7 @@ "Serilog": { "Using": [ "AWS.Logger.SeriLog" ], "MinimumLevel": "Debug", - "LogGroup": "AWSSeriLogGroupOverrideLogStreamName", + "LogGroup": "{LOG_GROUP_NAME}", "Region": "us-west-2", "LogStreamName": "CustomLogStream", "LogStreamNameSuffix": "Custom", diff --git a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupRestrictedToMinimumLevel.json b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupRestrictedToMinimumLevel.json index 466e7bd..4b5a1eb 100644 --- a/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupRestrictedToMinimumLevel.json +++ b/test/AWS.Logger.SeriLog.Tests/AWSSeriLogGroupRestrictedToMinimumLevel.json @@ -2,7 +2,7 @@ "Serilog": { "Using": [ "AWS.Logger.SeriLog" ], "MinimumLevel": "Debug", - "LogGroup": "AWSSeriLogGroupRestrictedtoMinimumLevel", + "LogGroup": "{LOG_GROUP_NAME}", "Region": "us-west-2", "LogStreamNameSuffix": "Custom", "LogStreamNamePrefix": "CustomPrefix", diff --git a/test/AWS.Logger.SeriLog.Tests/SeriLoggerTestClass.cs b/test/AWS.Logger.SeriLog.Tests/SeriLoggerTestClass.cs index 51abedf..84fd98f 100644 --- a/test/AWS.Logger.SeriLog.Tests/SeriLoggerTestClass.cs +++ b/test/AWS.Logger.SeriLog.Tests/SeriLoggerTestClass.cs @@ -1,8 +1,12 @@ using System; -using System.Threading; -using Amazon.CloudWatchLogs.Model; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Amazon.CloudWatchLogs.Model; using AWS.Logger.SeriLog; -using AWS.Logger.TestUtils; +using AWS.Logger.TestUtils; using Microsoft.Extensions.Configuration; using Serilog; using Xunit; @@ -18,98 +22,113 @@ public SeriLoggerTestClass(TestFixture testFixture) : base(testFixture) { } - private void CreateLoggerFromConfiguration(string configurationFile) + private void CreateLoggerFromConfiguration(string configurationFile, string logGroupName) { - var configuration = new ConfigurationBuilder() - .AddJsonFile(configurationFile) - .Build(); - - Log.Logger = new LoggerConfiguration(). - ReadFrom.Configuration(configuration). - WriteTo.AWSSeriLog( configuration).CreateLogger(); + var fileInfo = new FileInfo(configurationFile); + var fileContent = File.ReadAllText(fileInfo.FullName); + using (Stream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent.Replace("{LOG_GROUP_NAME}", logGroupName)))) + { + var configuration = new ConfigurationBuilder() + .AddJsonStream(memoryStream) + .Build(); + + Log.Logger = new LoggerConfiguration(). + ReadFrom.Configuration(configuration). + WriteTo.AWSSeriLog(configuration).CreateLogger(); + } } #region Test Cases [Fact] - public void SeriLogger() + public async Task SeriLogger() { - CreateLoggerFromConfiguration("AWSSeriLogGroup.json"); - SimpleLoggingTest("AWSSeriLogGroup"); + var logGroupName = $"AWSSeriLogGroup{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("AWSSeriLogGroup.json", logGroupName); + await SimpleLoggingTest(logGroupName); } [Fact] - public void MultiThreadTest() + public async Task MultiThreadTest() { - CreateLoggerFromConfiguration("AWSSeriLogGroupMultiThreadTest.json"); - MultiThreadTestGroup("AWSSeriLogGroupMultiThreadTest"); + var logGroupName = $"AWSSeriLogGroup{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("AWSSeriLogGroupMultiThreadTest.json", logGroupName); + await MultiThreadTestGroup(logGroupName); } [Fact] - public void MultiThreadBufferFullTest() + public async Task MultiThreadBufferFullTest() { - CreateLoggerFromConfiguration("AWSSeriLogGroupMultiThreadBufferFullTest.json"); - MultiThreadBufferFullTestGroup("AWSSeriLogGroupMultiThreadBufferFullTest"); - } - - /// - /// Verifies that multiple producers can log to the same log stream - /// when an override log stream name is provided - /// + var logGroupName = $"AWSSeriLogGroup{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("AWSSeriLogGroupMultiThreadBufferFullTest.json", logGroupName); + await MultiThreadBufferFullTestGroup(logGroupName); + } + + /// + /// Verifies that multiple producers can log to the same log stream + /// when an override log stream name is provided + /// [Fact] - public void CustomLogStreamNameTest() + public async Task CustomLogStreamNameTest() { - CreateLoggerFromConfiguration("AWSSeriLogGroupOverrideLogStreamName.json"); - MultiThreadTestGroup("AWSSeriLogGroupOverrideLogStreamName", "CustomLogStream"); + var logGroupName = $"AWSSeriLogGroup{Guid.NewGuid().ToString().Split('-').Last()}"; + CreateLoggerFromConfiguration("AWSSeriLogGroupOverrideLogStreamName.json", logGroupName); + await MultiThreadTestGroup(logGroupName, "CustomLogStream"); } [Fact] - public void RestrictedToMinimumLevelTest() + public async Task RestrictedToMinimumLevelTest() + { + var logGroupName = $"AWSSeriLogGroupRestrictedtoMinimumLevel{Guid.NewGuid().ToString().Split('-').Last()}"; + + var fileInfo = new FileInfo("AWSSeriLogGroupRestrictedToMinimumLevel.json"); + var fileContent = File.ReadAllText(fileInfo.FullName); + using (Stream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent.Replace("{LOG_GROUP_NAME}", logGroupName)))) + { + // Create logger + var configuration = new ConfigurationBuilder() + .AddJsonStream(memoryStream) + .Build(); + + Log.Logger = new LoggerConfiguration(). + ReadFrom.Configuration(configuration).CreateLogger(); + } + + await ExecuteRestrictedToMinimumLevelTest(logGroupName); + } + + private async Task ExecuteRestrictedToMinimumLevelTest(string logGroupName) { - string logGroupName = "AWSSeriLogGroupRestrictedtoMinimumLevel"; - // Create logger - var configuration = new ConfigurationBuilder() - .AddJsonFile("AWSSeriLogGroupRestrictedToMinimumLevel.json") - .Build(); - - Log.Logger = new LoggerConfiguration(). - ReadFrom.Configuration(configuration).CreateLogger(); - - ExecuteRestrictedToMinimumLevelTest(logGroupName); - } - - private void ExecuteRestrictedToMinimumLevelTest(string logGroupName) - { - // Log 4 Debug messages + // Log 4 Debug messages for (int i = 0; i < 3; i++) { - Log.Debug(string.Format("Test logging message {0} SeriLog, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId)); + Log.Debug(string.Format("Test logging message {0} SeriLog, Thread Id:{1}", i, Environment.CurrentManagedThreadId)); } // Log 5 Error messages for (int i = 0; i < 5; i++) { - Log.Error(string.Format("Test logging message {0} SeriLog, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId)); + Log.Error(string.Format("Test logging message {0} SeriLog, Thread Id:{1}", i, Environment.CurrentManagedThreadId)); } Log.Error(LASTMESSAGE); GetLogEventsResponse getLogEventsResponse = new GetLogEventsResponse(); - if (NotifyLoggingCompleted("AWSSeriLogGroupRestrictedtoMinimumLevel", "LASTMESSAGE")) + if (await NotifyLoggingCompleted(logGroupName, "LASTMESSAGE")) { - DescribeLogStreamsResponse describeLogstreamsResponse = Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest - { - Descending = true, - LogGroupName = logGroupName, - OrderBy = "LastEventTime" - }).Result; - - getLogEventsResponse = Client.GetLogEventsAsync(new GetLogEventsRequest + DescribeLogStreamsResponse describeLogstreamsResponse = await _testFixture.Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest + { + Descending = true, + LogGroupName = logGroupName, + OrderBy = "LastEventTime" + }); + + getLogEventsResponse = await _testFixture.Client.GetLogEventsAsync(new GetLogEventsRequest { LogGroupName = logGroupName, LogStreamName = describeLogstreamsResponse.LogStreams[0].LogStreamName - }).Result; + }); } - Assert.Equal(6, getLogEventsResponse.Events.Count); - } - + Assert.Equal(6, getLogEventsResponse.Events.Count); + } + /// /// This method posts debug messages onto CloudWatchLogs. /// @@ -118,7 +137,7 @@ protected override void LogMessages(int count) { for (int i = 0; i < count - 1; i++) { - Log.Debug(string.Format("Test logging message {0} SeriLog, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId)); + Log.Debug(string.Format("Test logging message {0} SeriLog, Thread Id:{1}", i, Environment.CurrentManagedThreadId)); } Log.Debug(LASTMESSAGE); } diff --git a/test/AWS.Logger.TestUtils/BaseTestClass.cs b/test/AWS.Logger.TestUtils/BaseTestClass.cs index 1c30223..99c3cc0 100644 --- a/test/AWS.Logger.TestUtils/BaseTestClass.cs +++ b/test/AWS.Logger.TestUtils/BaseTestClass.cs @@ -1,11 +1,8 @@ -using Amazon.CloudWatchLogs; -using Amazon.CloudWatchLogs.Model; +using Amazon.CloudWatchLogs.Model; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Text; -using System.Threading; using System.Threading.Tasks; using Xunit; @@ -21,41 +18,39 @@ public abstract class BaseTestClass : IClassFixture public const string CUSTOMSTREAMSUFFIX = "Custom"; public const string CUSTOMSTREAMPREFIX = "CustomPrefix"; public TestFixture _testFixture; - public AmazonCloudWatchLogsClient Client; public BaseTestClass(TestFixture testFixture) { _testFixture = testFixture; - Client = new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2); } - protected bool NotifyLoggingCompleted(string logGroupName, string filterPattern) + protected async Task NotifyLoggingCompleted(string logGroupName, string filterPattern) { Stopwatch timer = new Stopwatch(); timer.Start(); while (timer.Elapsed < TimeSpan.FromSeconds(THREAD_WAITTIME)) { - Thread.Sleep(500); - if (FilterPatternExists(logGroupName, filterPattern)) + await Task.Delay(500); + if (await FilterPatternExists(logGroupName, filterPattern)) { break; } } - return FilterPatternExists(logGroupName, filterPattern); + return await FilterPatternExists(logGroupName, filterPattern); } - protected bool FilterPatternExists(string logGroupName, string filterPattern) + protected async Task FilterPatternExists(string logGroupName, string filterPattern) { DescribeLogStreamsResponse describeLogstreamsResponse; try { - describeLogstreamsResponse = Client. + describeLogstreamsResponse = await _testFixture.Client. DescribeLogStreamsAsync(new DescribeLogStreamsRequest { Descending = true, LogGroupName = logGroupName, OrderBy = "LastEventTime" - }).Result; + }); } catch (Exception) { return false; @@ -63,15 +58,13 @@ protected bool FilterPatternExists(string logGroupName, string filterPattern) if (describeLogstreamsResponse.LogStreams.Count > 0) { - List logStreamNames = new List(); - logStreamNames.Add(describeLogstreamsResponse.LogStreams[0].LogStreamName); - FilterLogEventsResponse filterLogEventsResponse = Client. + FilterLogEventsResponse filterLogEventsResponse = await _testFixture.Client. FilterLogEventsAsync(new FilterLogEventsRequest { FilterPattern = filterPattern, LogGroupName = logGroupName, - LogStreamNames = logStreamNames - }).Result; + LogStreamNames = new List { describeLogstreamsResponse.LogStreams[0].LogStreamName } + }); return filterLogEventsResponse.Events.Count > 0; } @@ -83,33 +76,32 @@ protected bool FilterPatternExists(string logGroupName, string filterPattern) protected abstract void LogMessages(int count); - protected void SimpleLoggingTest(string logGroupName) + protected async Task SimpleLoggingTest(string logGroupName) { LogMessages(SIMPLELOGTEST_COUNT); GetLogEventsResponse getLogEventsResponse = new GetLogEventsResponse(); - if (NotifyLoggingCompleted(logGroupName, "LASTMESSAGE")) + if (await NotifyLoggingCompleted(logGroupName, "LASTMESSAGE")) { - DescribeLogStreamsResponse describeLogstreamsResponse = - Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest - { - Descending = true, - LogGroupName = logGroupName, - OrderBy = "LastEventTime" - }).Result; - - - getLogEventsResponse = Client.GetLogEventsAsync(new GetLogEventsRequest + var describeLogstreamsResponse = await _testFixture.Client.DescribeLogStreamsAsync( + new DescribeLogStreamsRequest + { + Descending = true, + LogGroupName = logGroupName, + OrderBy = "LastEventTime" + }); + var logStream = describeLogstreamsResponse.LogStreams.First(); + getLogEventsResponse = await _testFixture.Client.GetLogEventsAsync(new GetLogEventsRequest { LogGroupName = logGroupName, - LogStreamName = describeLogstreamsResponse.LogStreams[0].LogStreamName - }).Result; + LogStreamName = logStream.LogStreamName + }); - var customStreamSuffix = describeLogstreamsResponse.LogStreams[0].LogStreamName.Split('-').Last().Trim(); + var customStreamSuffix = logStream.LogStreamName.Split('-').Last().Trim(); Assert.Equal(CUSTOMSTREAMSUFFIX, customStreamSuffix); - var customStreamPrefix = describeLogstreamsResponse.LogStreams[0].LogStreamName.Split('-').First().Trim(); + var customStreamPrefix = logStream.LogStreamName.Split('-').First().Trim(); Assert.Equal(CUSTOMSTREAMPREFIX, customStreamPrefix); } - Assert.Equal(SIMPLELOGTEST_COUNT, getLogEventsResponse.Events.Count()); + Assert.Equal(SIMPLELOGTEST_COUNT, getLogEventsResponse.Events.Count); _testFixture.LogGroupNameList.Add(logGroupName); @@ -125,7 +117,7 @@ protected void SimpleLoggingTest(string logGroupName) /// May be used when overriding the stream name as opposed to using the /// generated name based on the prefix and suffix. /// - protected void MultiThreadTestGroup(string logGroupName, string expectedLogStreamName = "") + protected async Task MultiThreadTestGroup(string logGroupName, string expectedLogStreamName = "") { // This allows the fixture to delete the group at the end, // whether or not the test passes @@ -137,22 +129,21 @@ protected void MultiThreadTestGroup(string logGroupName, string expectedLogStrea var totalCount = 0; for (int i = 0; i < THREAD_COUNT; i++) { - tasks.Add(Task.Factory.StartNew(() => LogMessages(count))); + tasks.Add(Task.Run(() => LogMessages(count))); totalCount = totalCount + count; } - - Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(THREAD_WAITTIME)); + await Task.WhenAll(tasks.ToArray()); int testCount = -1; - if (NotifyLoggingCompleted(logGroupName, "LASTMESSAGE")) + if (await NotifyLoggingCompleted(logGroupName, "LASTMESSAGE")) { DescribeLogStreamsResponse describeLogstreamsResponse = - Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest + await _testFixture.Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest { Descending = true, LogGroupName = logGroupName, OrderBy = "LastEventTime" - }).Result; + }); if (describeLogstreamsResponse.LogStreams.Count > 0) { @@ -164,19 +155,16 @@ protected void MultiThreadTestGroup(string logGroupName, string expectedLogStrea Assert.Equal(expectedLogStreamName, describeLogstreamsResponse.LogStreams[0].LogStreamName); } testCount = 0; - foreach (var logStream in describeLogstreamsResponse.LogStreams) - { - GetLogEventsResponse getLogEventsResponse = - Client.GetLogEventsAsync(new GetLogEventsRequest + GetLogEventsResponse getLogEventsResponse = + await _testFixture.Client.GetLogEventsAsync(new GetLogEventsRequest { LogGroupName = logGroupName, - LogStreamName = logStream.LogStreamName - }).Result; + LogStreamName = describeLogstreamsResponse.LogStreams[0].LogStreamName + }); - if (getLogEventsResponse != null) - { - testCount += getLogEventsResponse.Events.Count(); - } + if (getLogEventsResponse != null) + { + testCount += getLogEventsResponse.Events.Count; } } } @@ -184,7 +172,7 @@ protected void MultiThreadTestGroup(string logGroupName, string expectedLogStrea Assert.Equal(totalCount, testCount); } - protected void MultiThreadBufferFullTestGroup(string logGroupName) + protected async Task MultiThreadBufferFullTestGroup(string logGroupName) { var tasks = new List(); var streamNames = new List(); @@ -192,11 +180,11 @@ protected void MultiThreadBufferFullTestGroup(string logGroupName) var totalCount = 0; for (int i = 0; i < THREAD_COUNT; i++) { - tasks.Add(Task.Factory.StartNew(() => LogMessages(count))); + tasks.Add(Task.Run(() => LogMessages(count))); totalCount = totalCount + count; } - Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(THREAD_WAITTIME)); - Assert.True(NotifyLoggingCompleted(logGroupName, "maximum")); + await Task.WhenAll(tasks.ToArray()); + Assert.True(await NotifyLoggingCompleted(logGroupName, "maximum")); _testFixture.LogGroupNameList.Add(logGroupName); } diff --git a/test/AWS.Logger.TestUtils/TestFixture.cs b/test/AWS.Logger.TestUtils/TestFixture.cs index e0f1f9c..aa49c23 100644 --- a/test/AWS.Logger.TestUtils/TestFixture.cs +++ b/test/AWS.Logger.TestUtils/TestFixture.cs @@ -1,12 +1,9 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; -using Amazon; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; -using System.Threading; -using System.Diagnostics; +using Xunit; namespace AWS.Logger.TestUtils { @@ -15,45 +12,46 @@ namespace AWS.Logger.TestUtils // In the Build tab select "Produce outputs on build". //TestClass to dispose test generated LogGroups. - public class TestFixture : IDisposable + public class TestFixture : IAsyncLifetime { public List LogGroupNameList; + public AmazonCloudWatchLogsClient Client; - public TestFixture() + public Task InitializeAsync() { - AmazonCloudWatchLogsClient Client = - new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2); + Client = new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2); LogGroupNameList = new List(); - } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); + return Task.CompletedTask; } - protected virtual void Dispose(bool disposing) + public async Task DisposeAsync() { - AmazonCloudWatchLogsClient Client = - new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.USWest2); foreach (var logGroupName in LogGroupNameList) { - if (!(string.IsNullOrEmpty(logGroupName))) + try { - DescribeLogGroupsResponse describeLogGroupsResponse = Client.DescribeLogGroupsAsync( - new DescribeLogGroupsRequest + if (!(string.IsNullOrEmpty(logGroupName))) { - LogGroupNamePrefix = logGroupName - }).Result; + var describeLogGroupsResponse = await Client.DescribeLogGroupsAsync( + new DescribeLogGroupsRequest + { + LogGroupNamePrefix = logGroupName + }); - if (!(string.IsNullOrEmpty(describeLogGroupsResponse.LogGroups[0].LogGroupName))) - { - var response = Client.DeleteLogGroupAsync(new DeleteLogGroupRequest + foreach (var logGroup in describeLogGroupsResponse.LogGroups) { - LogGroupName = logGroupName - }).Result; + if (!(string.IsNullOrEmpty(logGroup.LogGroupName))) + { + var response = await Client.DeleteLogGroupAsync(new DeleteLogGroupRequest + { + LogGroupName = logGroup.LogGroupName + }); + } + } } } + catch (Exception) { } } } }