Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .autover/changes/848f00a3-61fa-4bd4-a920-da5a32851f68.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Projects": [
{
"Name": "AWS.Logger.Core",
"Type": "Patch",
"ChangelogMessages": [
"Add the property PreconfiguredServiceClient to AWSLoggerConfig to make it easier to test special scenarios",
"Update dependency version of AWSSDK.CloudWatchLogs to 4.0.13.3 from 4.0.8.4"
]
}
]
}
2 changes: 1 addition & 1 deletion samples/AspNetCore/ConsoleSample/ConsoleSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="4.0.0.32" />
<PackageReference Include="AWSSDK.Core" Version="4.0.3.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="4.0.0.32" />
<PackageReference Include="AWSSDK.Core" Version="4.0.3.6" />
<PackageReference Include="log4net" Version="2.0.15" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/AWS.Logger.Core/AWS.Logger.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="4.0.8.4" />
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="4.0.13.3" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

Expand Down
8 changes: 7 additions & 1 deletion src/AWS.Logger.Core/AWSLoggerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;

using Amazon.CloudWatchLogs;
using Amazon.Runtime;
namespace AWS.Logger
{
Expand Down Expand Up @@ -215,5 +215,11 @@ public AWSLoggerConfig(string logGroup)
/// change it only if the region cannot be determined from the service endpoint.
/// </summary>
public string AuthenticationRegion { get; set; }

/// <summary>
/// An IAmazonCloudWatchLogs that has already been configured to connect to Amazon CloudWatch Logs. This property
/// is general used for testing purposes testing special scenarios.
/// </summary>
public IAmazonCloudWatchLogs PreconfiguredServiceClient { get; set; }
}
}
66 changes: 42 additions & 24 deletions src/AWS.Logger.Core/Core/AWSLoggerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,57 @@ public AWSLoggerCore(AWSLoggerConfig config, string logType)
_config = config;
_logType = logType;

var awsConfig = new AmazonCloudWatchLogsConfig();
if (!string.IsNullOrWhiteSpace(_config.ServiceUrl))
if (config.PreconfiguredServiceClient == null)
{
var serviceUrl = _config.ServiceUrl.Trim();
awsConfig.ServiceURL = serviceUrl;
if (serviceUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
var awsConfig = new AmazonCloudWatchLogsConfig();
if (!string.IsNullOrWhiteSpace(_config.ServiceUrl))
{
awsConfig.UseHttp = true;
var serviceUrl = _config.ServiceUrl.Trim();
awsConfig.ServiceURL = serviceUrl;
if (serviceUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
awsConfig.UseHttp = true;
}
}
}
else
{
if (!string.IsNullOrEmpty(_config.Region))
else
{
awsConfig.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(_config.Region);
if (!string.IsNullOrEmpty(_config.Region))
{
awsConfig.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(_config.Region);
}
}
}

if (!string.IsNullOrEmpty(_config.AuthenticationRegion))
{
awsConfig.AuthenticationRegion = _config.AuthenticationRegion;
}
if (!string.IsNullOrEmpty(_config.AuthenticationRegion))
{
awsConfig.AuthenticationRegion = _config.AuthenticationRegion;
}

_client = new Lazy<IAmazonCloudWatchLogs>(() =>
{
var credentials = DetermineCredentials(config);
var client = new AmazonCloudWatchLogsClient(credentials, awsConfig);

client.BeforeRequestEvent += ServiceClientBeforeRequestEvent;
client.ExceptionEvent += ServiceClientExceptionEvent;

_client = new Lazy<IAmazonCloudWatchLogs>(() =>
return client;
});
}
else
{
var credentials = DetermineCredentials(config);
var client = new AmazonCloudWatchLogsClient(credentials, awsConfig);
var preconfiguredClient = config.PreconfiguredServiceClient;
if (preconfiguredClient is AmazonCloudWatchLogsClient preconfiguredClientImpl)
{
preconfiguredClientImpl.BeforeRequestEvent += ServiceClientBeforeRequestEvent;
preconfiguredClientImpl.ExceptionEvent += ServiceClientExceptionEvent;
}

client.BeforeRequestEvent += ServiceClientBeforeRequestEvent;
client.ExceptionEvent += ServiceClientExceptionEvent;

return client;
});
_client = new Lazy<IAmazonCloudWatchLogs>(() =>
{
return preconfiguredClient;
});
}

StartMonitor();
RegisterShutdownHook();
Expand Down Expand Up @@ -681,7 +699,7 @@ void ServiceClientBeforeRequestEvent(object sender, RequestEventArgs e)
if (args != null && args.Request is Amazon.Runtime.Internal.IAmazonWebServiceRequest internalRequest && !internalRequest.UserAgentDetails.GetCustomUserAgentComponents().Contains(userAgentString))
{
internalRequest.UserAgentDetails.AddUserAgentComponent(userAgentString);
}
}
}

void ServiceClientExceptionEvent(object sender, ExceptionEventArgs e)
Expand Down
70 changes: 70 additions & 0 deletions test/AWS.Logger.AspNetCore.Tests/TestUserAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
using Amazon.Runtime;
using AWS.Logger.Core;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

namespace AWS.Logger.AspNetCore.Tests
{
public class TestUserAgent
{
[Fact]
public async Task VerifyUserAgent()
{
var client = new AmazonCloudWatchLogsClient();

var userAgentCaptured = new List<string>();
client.AfterResponseEvent += (sender, args) =>
{
var we = args as WebServiceResponseEventArgs;
if (we == null || we.Response is not PutLogEventsResponse)
return;

var userAgent = we.Response.ResponseMetadata.Metadata["User-Agent"];
userAgentCaptured.Add(userAgent);
};

var core = new AWSLoggerCore(new AWSLoggerConfig
{
LogGroup = "/aws/logging-tests/verify-useragent",
PreconfiguredServiceClient = client
}, "aws-logger-aspnetcore#0.0.0.0");

core.AddMessage("Test message for User-Agent verification");
core.Flush();

var start = DateTime.UtcNow;
while (DateTime.UtcNow < start.AddSeconds(10))
{
if (userAgentCaptured.Count == 1)
break;

await Task.Delay(100);
}

Assert.Single(userAgentCaptured);
Assert.Contains("ft/aws-logger-aspnetcore#0.0.0.0", userAgentCaptured[0]);

core.AddMessage("Test message for User-Agent verification");
core.Flush();

start = DateTime.UtcNow;
while (DateTime.UtcNow < start.AddSeconds(10))
{
if (userAgentCaptured.Count == 2)
break;

await Task.Delay(100);
}

Assert.Equal(2, userAgentCaptured.Count);
// The user agent should not change between calls. This is verify the bug fix https://github.com/aws/aws-logging-dotnet/issues/340
// where the user agent string kept growing with each call. Logging library was more susceptible this because it reuses the same underlying
// PutLogEventsRequest instance for each push to send logs.
Assert.Equal(userAgentCaptured[0], userAgentCaptured[1]);
}
}
}
Loading