Skip to content

Commit

Permalink
Fixed an issue where restrictedToMinimumLevel from appsettings.json w…
Browse files Browse the repository at this point in the history
…as ignored. (#168)
  • Loading branch information
ashishdhingra committed Jun 24, 2021
1 parent f90ae5d commit 105617b
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 5 deletions.
24 changes: 24 additions & 0 deletions AWS.Logger.sln
Expand Up @@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AspNetCore", "AspNetCore",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Log4net", "Log4net", "{D28DFB15-FA6F-42EE-890C-E6300A187CCA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Serilog", "Serilog", "{87F72A08-D504-4DB5-91DC-CDD4F8C6B85D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AWS.Logger.Log4net", "src\AWS.Logger.Log4net\AWS.Logger.Log4net.csproj", "{78312E9B-BC7C-4AF8-9153-DD72BBF4FD36}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLog.AWS.Logger", "src\NLog.AWS.Logger\NLog.AWS.Logger.csproj", "{5BBF70EA-A239-4A42-B1E6-83ADCC01153B}"
Expand Down Expand Up @@ -61,6 +63,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicAWSCredentialsConfigur
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSample", "samples\AspNetCore\WebSample\WebSample.csproj", "{95F48BA5-D01C-47F1-9D66-3A6D122A314E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerilogTestCode", "samples\Serilog\SerilogTestCode\SerilogTestCode.csproj", "{72EE670C-BEF1-4319-8F64-209FF12528E2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerilogTestCodeFromConfig", "samples\Serilog\SerilogTestCodeFromConfig\SerilogTestCodeFromConfig.csproj", "{87B82DF2-003B-4A83-AA44-5420714D6C70}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerilogTestCodeFromConfigRestrictedToMinimumLevel", "samples\Serilog\SerilogTestCodeFromConfigRestrictedToMinimumLevel\SerilogTestCodeFromConfigRestrictedToMinimumLevel.csproj", "{69DE761E-E9E4-4A65-B71E-D23AA4A1AF36}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -147,6 +155,18 @@ Global
{95F48BA5-D01C-47F1-9D66-3A6D122A314E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95F48BA5-D01C-47F1-9D66-3A6D122A314E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95F48BA5-D01C-47F1-9D66-3A6D122A314E}.Release|Any CPU.Build.0 = Release|Any CPU
{72EE670C-BEF1-4319-8F64-209FF12528E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72EE670C-BEF1-4319-8F64-209FF12528E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72EE670C-BEF1-4319-8F64-209FF12528E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72EE670C-BEF1-4319-8F64-209FF12528E2}.Release|Any CPU.Build.0 = Release|Any CPU
{87B82DF2-003B-4A83-AA44-5420714D6C70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87B82DF2-003B-4A83-AA44-5420714D6C70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87B82DF2-003B-4A83-AA44-5420714D6C70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87B82DF2-003B-4A83-AA44-5420714D6C70}.Release|Any CPU.Build.0 = Release|Any CPU
{69DE761E-E9E4-4A65-B71E-D23AA4A1AF36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69DE761E-E9E4-4A65-B71E-D23AA4A1AF36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69DE761E-E9E4-4A65-B71E-D23AA4A1AF36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69DE761E-E9E4-4A65-B71E-D23AA4A1AF36}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -155,6 +175,7 @@ Global
{7A064171-6FA7-44CC-9819-2C383A3195D9} = {2E63B9B6-CE21-41D3-8B54-51454D8FA0BB}
{207FA88A-9F32-45A0-84AC-838383875A55} = {2E63B9B6-CE21-41D3-8B54-51454D8FA0BB}
{D28DFB15-FA6F-42EE-890C-E6300A187CCA} = {2E63B9B6-CE21-41D3-8B54-51454D8FA0BB}
{87F72A08-D504-4DB5-91DC-CDD4F8C6B85D} = {2E63B9B6-CE21-41D3-8B54-51454D8FA0BB}
{78312E9B-BC7C-4AF8-9153-DD72BBF4FD36} = {E3018A04-6209-4500-B8F2-706514407F90}
{5BBF70EA-A239-4A42-B1E6-83ADCC01153B} = {E3018A04-6209-4500-B8F2-706514407F90}
{71373EC1-B7CD-41E5-9AEE-2D58269079A2} = {7A064171-6FA7-44CC-9819-2C383A3195D9}
Expand All @@ -175,6 +196,9 @@ Global
{916D3A6A-3713-48FB-8978-1440CD02F782} = {E3018A04-6209-4500-B8F2-706514407F90}
{7249C7F3-A0EF-4940-9931-3096C87E009E} = {D28DFB15-FA6F-42EE-890C-E6300A187CCA}
{95F48BA5-D01C-47F1-9D66-3A6D122A314E} = {207FA88A-9F32-45A0-84AC-838383875A55}
{72EE670C-BEF1-4319-8F64-209FF12528E2} = {87F72A08-D504-4DB5-91DC-CDD4F8C6B85D}
{87B82DF2-003B-4A83-AA44-5420714D6C70} = {87F72A08-D504-4DB5-91DC-CDD4F8C6B85D}
{69DE761E-E9E4-4A65-B71E-D23AA4A1AF36} = {87F72A08-D504-4DB5-91DC-CDD4F8C6B85D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {46F50BDA-08A5-4A81-9CA9-F0B732C341A7}
Expand Down
@@ -0,0 +1,23 @@
using Serilog;
using Microsoft.Extensions.Configuration;

namespace SerilogTestCodeFromConfigRestrictedToMinimumLevel
{
class Program
{
static void Main(string[] args)
{
// logger configuration reads from appsettings.json
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

logger.Information("This should not log since restrictedToMinimumLevel is Error in appsettings.json!");
logger.Error("Hello!");
}
}
}
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWS.Logger.SeriLog" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
@@ -0,0 +1,18 @@
{
"Serilog": {
"Using": [
"AWS.Logger.SeriLog"
],
"LogGroup": "Serilog.ConfigExample",
"Region": "us-east-1",
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "AWSSeriLog",
"Args": {
"restrictedToMinimumLevel": "Error"
}
}
]
}
}
4 changes: 2 additions & 2 deletions src/AWS.Logger.SeriLog/AWS.Logger.SeriLog.csproj
Expand Up @@ -21,7 +21,7 @@

<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\awssdk.dll.snk</AssemblyOriginatorKeyFile>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Serilog" Version="2.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/AWS.Logger.SeriLog/AWSLoggerSeriLogExtension.cs
Expand Up @@ -89,7 +89,7 @@ public static class AWSLoggerSeriLogExtension
{
config.LibraryLogErrors = Boolean.Parse(configuration[LIBRARY_LOG_ERRORS]);
}
return AWSSeriLog(loggerConfiguration, config, iFormatProvider, textFormatter);
return AWSSeriLog(loggerConfiguration, config, iFormatProvider, textFormatter, restrictedToMinimumLevel);
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions test/AWS.Logger.SeriLog.Tests/AWS.Logger.SeriLog.Tests.csproj
Expand Up @@ -11,9 +11,13 @@
<ItemGroup>
<None Remove="AWSNLogGroupMultiThreadBufferFullTest.json" />
<None Remove="AWSNLogGroupMultiThreadTest.json" />
<None Remove="AWSSeriLogGroupRestrictedToMinimumLevel.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="AWSSeriLogGroupRestrictedToMinimumLevel.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="AWSSeriLogGroup.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
Expand All @@ -32,8 +36,8 @@

<ItemGroup>
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Serilog.Settings.Configuration" Version="2.5.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<!-- This needs to be referenced to allow testing via AssumeRole credentials -->
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.0.5" />
</ItemGroup>
Expand Down
@@ -0,0 +1,18 @@
{
"Serilog": {
"Using": [ "AWS.Logger.SeriLog" ],
"MinimumLevel": "Debug",
"LogGroup": "AWSSeriLogGroupRestrictedtoMinimumLevel",
"Region": "us-west-2",
"LogStreamNameSuffix": "Custom",
"LogStreamNamePrefix": "CustomPrefix",
"WriteTo": [
{
"Name": "AWSSeriLog",
"Args": {
"restrictedToMinimumLevel": "Error"
}
}
]
}
}
49 changes: 49 additions & 0 deletions test/AWS.Logger.SeriLog.Tests/SeriLoggerTestClass.cs
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using Amazon.CloudWatchLogs.Model;
using AWS.Logger.SeriLog;
using AWS.Logger.TestUtils;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -50,6 +51,54 @@ public void MultiThreadBufferFullTest()
MultiThreadBufferFullTestGroup("AWSSeriLogGroupMultiThreadBufferFullTest");
}

[Fact]
public void RestrictedToMinimumLevelTest()
{
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
for (int i = 0; i < 3; i++)
{
Log.Debug(string.Format("Test logging message {0} SeriLog, Thread Id:{1}", i, Thread.CurrentThread.ManagedThreadId));
}
// 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(LASTMESSAGE);

GetLogEventsResponse getLogEventsResponse = new GetLogEventsResponse();
if (NotifyLoggingCompleted("AWSSeriLogGroupRestrictedtoMinimumLevel", "LASTMESSAGE"))
{
DescribeLogStreamsResponse describeLogstreamsResponse = Client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest
{
Descending = true,
LogGroupName = logGroupName,
OrderBy = "LastEventTime"
}).Result;

getLogEventsResponse = Client.GetLogEventsAsync(new GetLogEventsRequest
{
LogGroupName = logGroupName,
LogStreamName = describeLogstreamsResponse.LogStreams[0].LogStreamName
}).Result;
}
Assert.Equal(6, getLogEventsResponse.Events.Count);
}

/// <summary>
/// This method posts debug messages onto CloudWatchLogs.
/// </summary>
Expand Down

0 comments on commit 105617b

Please sign in to comment.