Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Releasing #269 #271

Merged
merged 3 commits into from
May 22, 2024
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
2 changes: 1 addition & 1 deletion src/AWS.Logger.AspNetCore/AWS.Logger.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
<Version>3.5.2</Version>
<Version>3.5.3</Version>
</PropertyGroup>

<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 @@ -23,7 +23,7 @@
<SignAssembly>True</SignAssembly>
<DelaySign>False</DelaySign>
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
<Version>3.3.2</Version>
<Version>3.3.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
47 changes: 33 additions & 14 deletions src/AWS.Logger.Core/Core/AWSLoggerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,25 @@ public class AWSLoggerCore : IAWSLoggerCore
private SemaphoreSlim _flushTriggerEvent;
private ManualResetEventSlim _flushCompletedEvent;
private AWSLoggerConfig _config;
private IAmazonCloudWatchLogs _client;
private DateTime _maxBufferTimeStamp = new DateTime();
private string _logType;

/// <summary>
/// Internal CloudWatch Logs client
/// </summary>
/// <remarks>
/// We defer the initialization of the client until it is first accessed. This avoids a deadlock for log4net:
/// 1. The thread creating the logger (which contains the CWL client) gets an internal lock in log4net, then tries to
/// access SDK configuration via the static FallbackInternalConfigurationFactory.
/// 2. The timer thread the SDK uses to load EC2 IMDS credentials requests SDK configuration via
/// FallbackInternalConfigurationFactory, which attempts to create additional loggers for logging the configuration loading.
/// There's an implicit lock around FallbackInternalConfigurationFactory's static constructor, so these two threads deadlock.
///
/// By delaying initializing the internal client, we delay starting thread 2 until thread 1 has finished, that way we're
/// not creating additional log4net loggers in FallbackInternalConfigurationFactory while another thread is holding the log4net lock.
/// </remarks>
private Lazy<IAmazonCloudWatchLogs> _client;

private static readonly string _assemblyVersion = typeof(AWSLoggerCore).GetTypeInfo().Assembly.GetName().Version?.ToString() ?? string.Empty;
private static readonly string _baseUserAgentString = $"lib/aws-logger-core#{_assemblyVersion}";

Expand Down Expand Up @@ -102,12 +117,16 @@ public AWSLoggerCore(AWSLoggerConfig config, string logType)
awsConfig.AuthenticationRegion = _config.AuthenticationRegion;
}

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

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

((AmazonCloudWatchLogsClient)this._client).BeforeRequestEvent += ServiceClientBeforeRequestEvent;
((AmazonCloudWatchLogsClient)this._client).ExceptionEvent += ServiceClienExceptionEvent;
return client;
});

StartMonitor();
RegisterShutdownHook();
Expand Down Expand Up @@ -215,9 +234,9 @@ private string GetServiceUrl()
{
try
{
_client.Config.Validate();
_client.Value.Config.Validate();
#pragma warning disable CS0618 // Type or member is obsolete
return _client.Config.DetermineServiceURL() ?? "Undetermined ServiceURL";
return _client.Value.Config.DetermineServiceURL() ?? "Undetermined ServiceURL";
#pragma warning restore CS0618 // Type or member is obsolete
}
catch (Exception ex)
Expand Down Expand Up @@ -331,7 +350,7 @@ private async Task Monitor(CancellationToken token)
LogLibraryServiceError(ex);
if (token.IsCancellationRequested)
{
_client.Dispose();
_client.Value.Dispose();
return;
}
}
Expand Down Expand Up @@ -380,7 +399,7 @@ private async Task Monitor(CancellationToken token)
{
if (!token.IsCancellationRequested || !_repo.IsEmpty || !_pendingMessageQueue.IsEmpty)
LogLibraryServiceError(ex);
_client.Dispose();
_client.Value.Dispose();
return;
}
catch (Exception ex)
Expand All @@ -403,7 +422,7 @@ private async Task SendMessages(CancellationToken token)
{
//Make sure the log events are in the right order.
_repo._request.LogEvents.Sort((ev1, ev2) => ev1.Timestamp.CompareTo(ev2.Timestamp));
var response = await _client.PutLogEventsAsync(_repo._request, token).ConfigureAwait(false);
var response = await _client.Value.PutLogEventsAsync(_repo._request, token).ConfigureAwait(false);
_repo.Reset();
}
catch (ResourceNotFoundException ex)
Expand All @@ -425,7 +444,7 @@ private async Task<string> LogEventTransmissionSetup(CancellationToken token)

if (!_config.DisableLogGroupCreation)
{
var logGroupResponse = await _client.DescribeLogGroupsAsync(new DescribeLogGroupsRequest
var logGroupResponse = await _client.Value.DescribeLogGroupsAsync(new DescribeLogGroupsRequest
{
LogGroupNamePrefix = _config.LogGroup
}, token).ConfigureAwait(false);
Expand All @@ -436,7 +455,7 @@ private async Task<string> LogEventTransmissionSetup(CancellationToken token)

if (logGroupResponse.LogGroups.FirstOrDefault(x => string.Equals(x.LogGroupName, _config.LogGroup, StringComparison.Ordinal)) == null)
{
var createGroupResponse = await _client.CreateLogGroupAsync(new CreateLogGroupRequest { LogGroupName = _config.LogGroup }, token).ConfigureAwait(false);
var createGroupResponse = await _client.Value.CreateLogGroupAsync(new CreateLogGroupRequest { LogGroupName = _config.LogGroup }, token).ConfigureAwait(false);
if (!IsSuccessStatusCode(createGroupResponse))
{
LogLibraryServiceError(new System.Net.WebException($"Create LogGroup {_config.LogGroup} returned status: {createGroupResponse.HttpStatusCode}"), serviceURL);
Expand All @@ -454,7 +473,7 @@ private async Task<string> LogEventTransmissionSetup(CancellationToken token)

try
{
var streamResponse = await _client.CreateLogStreamAsync(new CreateLogStreamRequest
var streamResponse = await _client.Value.CreateLogStreamAsync(new CreateLogStreamRequest
{
LogGroupName = _config.LogGroup,
LogStreamName = currentStreamName
Expand Down Expand Up @@ -486,7 +505,7 @@ private void PutRetentionPolicy(int logGroupRetentionInDays, string logGroup, st
{
try
{
var putPolicyResponse = await _client.PutRetentionPolicyAsync(new PutRetentionPolicyRequest(logGroup, logGroupRetentionInDays), token).ConfigureAwait(false);
var putPolicyResponse = await _client.Value.PutRetentionPolicyAsync(new PutRetentionPolicyRequest(logGroup, logGroupRetentionInDays), token).ConfigureAwait(false);
if (!IsSuccessStatusCode(putPolicyResponse))
{
LogLibraryServiceError(new System.Net.WebException($"Put retention policy {logGroupRetentionInDays} for LogGroup {logGroup} returned status: {putPolicyResponse.HttpStatusCode}"), serviceURL);
Expand Down
2 changes: 1 addition & 1 deletion src/AWS.Logger.Core/IAWSLoggerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public interface IAWSLoggerConfig
bool LibraryLogErrors { get; set; }

/// <summary>
/// Gets and sets the LibraryLogFileName property. This is the name of the file into which errors from the AWS.Logger.Core library will be written into.
/// Gets and sets the LibraryLogFileName property. This is the name (and optional path) of the file into which errors from the AWS.Logger.Core library will be written into.
/// <para>
/// The default is going to "aws-logger-errors.txt".
/// </para>
Expand Down
2 changes: 1 addition & 1 deletion src/AWS.Logger.Log4net/AWS.Logger.Log4net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
<Version>3.5.2</Version>
<Version>3.5.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/AWS.Logger.Log4net/AWSAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public TimeSpan FlushTimeout
}

/// <summary>
/// Gets and sets the LibraryLogFileName property. This is the name of the file into which errors from the AWS.Logger.Core library will be written into.
/// Gets and sets the LibraryLogFileName property. This is the name (and optional path) of the file into which errors from the AWS.Logger.Core library will be written into.
/// <para>
/// The default is going to "aws-logger-errors.txt".
/// </para>
Expand Down
2 changes: 1 addition & 1 deletion src/AWS.Logger.SeriLog/AWS.Logger.SeriLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
<Version>3.4.2</Version>
<Version>3.4.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NLog.AWS.Logger/AWSTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public bool LibraryLogErrors
}

/// <summary>
/// Gets and sets the LibraryLogFileName property. This is the name of the file into which errors from the AWS.Logger.Core library will be written into.
/// Gets and sets the LibraryLogFileName property. This is the name (and optional path) of the file into which errors from the AWS.Logger.Core library will be written into.
/// <para>
/// The default is "aws-logger-errors.txt".
/// </para>
Expand Down
2 changes: 1 addition & 1 deletion src/NLog.AWS.Logger/NLog.AWS.Logger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
<Version>3.3.3</Version>
<Version>3.3.4</Version>
</PropertyGroup>

<ItemGroup>
Expand Down