Skip to content

Commit 1dbbb89

Browse files
authored
Merge pull request #271 from aws/dev
Releasing #269
2 parents 5c0c9ec + 5f92855 commit 1dbbb89

File tree

9 files changed

+41
-22
lines changed

9 files changed

+41
-22
lines changed

src/AWS.Logger.AspNetCore/AWS.Logger.AspNetCore.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<SignAssembly>True</SignAssembly>
2424
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
25-
<Version>3.5.2</Version>
25+
<Version>3.5.3</Version>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

src/AWS.Logger.Core/AWS.Logger.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<SignAssembly>True</SignAssembly>
2424
<DelaySign>False</DelaySign>
2525
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
26-
<Version>3.3.2</Version>
26+
<Version>3.3.3</Version>
2727
</PropertyGroup>
2828

2929
<ItemGroup>

src/AWS.Logger.Core/Core/AWSLoggerCore.cs

+33-14
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@ public class AWSLoggerCore : IAWSLoggerCore
3030
private SemaphoreSlim _flushTriggerEvent;
3131
private ManualResetEventSlim _flushCompletedEvent;
3232
private AWSLoggerConfig _config;
33-
private IAmazonCloudWatchLogs _client;
3433
private DateTime _maxBufferTimeStamp = new DateTime();
3534
private string _logType;
3635

36+
/// <summary>
37+
/// Internal CloudWatch Logs client
38+
/// </summary>
39+
/// <remarks>
40+
/// We defer the initialization of the client until it is first accessed. This avoids a deadlock for log4net:
41+
/// 1. The thread creating the logger (which contains the CWL client) gets an internal lock in log4net, then tries to
42+
/// access SDK configuration via the static FallbackInternalConfigurationFactory.
43+
/// 2. The timer thread the SDK uses to load EC2 IMDS credentials requests SDK configuration via
44+
/// FallbackInternalConfigurationFactory, which attempts to create additional loggers for logging the configuration loading.
45+
/// There's an implicit lock around FallbackInternalConfigurationFactory's static constructor, so these two threads deadlock.
46+
///
47+
/// By delaying initializing the internal client, we delay starting thread 2 until thread 1 has finished, that way we're
48+
/// not creating additional log4net loggers in FallbackInternalConfigurationFactory while another thread is holding the log4net lock.
49+
/// </remarks>
50+
private Lazy<IAmazonCloudWatchLogs> _client;
51+
3752
private static readonly string _assemblyVersion = typeof(AWSLoggerCore).GetTypeInfo().Assembly.GetName().Version?.ToString() ?? string.Empty;
3853
private static readonly string _baseUserAgentString = $"lib/aws-logger-core#{_assemblyVersion}";
3954

@@ -102,12 +117,16 @@ public AWSLoggerCore(AWSLoggerConfig config, string logType)
102117
awsConfig.AuthenticationRegion = _config.AuthenticationRegion;
103118
}
104119

105-
var credentials = DetermineCredentials(config);
106-
_client = new AmazonCloudWatchLogsClient(credentials, awsConfig);
120+
_client = new Lazy<IAmazonCloudWatchLogs>(() =>
121+
{
122+
var credentials = DetermineCredentials(config);
123+
var client = new AmazonCloudWatchLogsClient(credentials, awsConfig);
107124

125+
client.BeforeRequestEvent += ServiceClientBeforeRequestEvent;
126+
client.ExceptionEvent += ServiceClienExceptionEvent;
108127

109-
((AmazonCloudWatchLogsClient)this._client).BeforeRequestEvent += ServiceClientBeforeRequestEvent;
110-
((AmazonCloudWatchLogsClient)this._client).ExceptionEvent += ServiceClienExceptionEvent;
128+
return client;
129+
});
111130

112131
StartMonitor();
113132
RegisterShutdownHook();
@@ -215,9 +234,9 @@ private string GetServiceUrl()
215234
{
216235
try
217236
{
218-
_client.Config.Validate();
237+
_client.Value.Config.Validate();
219238
#pragma warning disable CS0618 // Type or member is obsolete
220-
return _client.Config.DetermineServiceURL() ?? "Undetermined ServiceURL";
239+
return _client.Value.Config.DetermineServiceURL() ?? "Undetermined ServiceURL";
221240
#pragma warning restore CS0618 // Type or member is obsolete
222241
}
223242
catch (Exception ex)
@@ -331,7 +350,7 @@ private async Task Monitor(CancellationToken token)
331350
LogLibraryServiceError(ex);
332351
if (token.IsCancellationRequested)
333352
{
334-
_client.Dispose();
353+
_client.Value.Dispose();
335354
return;
336355
}
337356
}
@@ -380,7 +399,7 @@ private async Task Monitor(CancellationToken token)
380399
{
381400
if (!token.IsCancellationRequested || !_repo.IsEmpty || !_pendingMessageQueue.IsEmpty)
382401
LogLibraryServiceError(ex);
383-
_client.Dispose();
402+
_client.Value.Dispose();
384403
return;
385404
}
386405
catch (Exception ex)
@@ -403,7 +422,7 @@ private async Task SendMessages(CancellationToken token)
403422
{
404423
//Make sure the log events are in the right order.
405424
_repo._request.LogEvents.Sort((ev1, ev2) => ev1.Timestamp.CompareTo(ev2.Timestamp));
406-
var response = await _client.PutLogEventsAsync(_repo._request, token).ConfigureAwait(false);
425+
var response = await _client.Value.PutLogEventsAsync(_repo._request, token).ConfigureAwait(false);
407426
_repo.Reset();
408427
}
409428
catch (ResourceNotFoundException ex)
@@ -425,7 +444,7 @@ private async Task<string> LogEventTransmissionSetup(CancellationToken token)
425444

426445
if (!_config.DisableLogGroupCreation)
427446
{
428-
var logGroupResponse = await _client.DescribeLogGroupsAsync(new DescribeLogGroupsRequest
447+
var logGroupResponse = await _client.Value.DescribeLogGroupsAsync(new DescribeLogGroupsRequest
429448
{
430449
LogGroupNamePrefix = _config.LogGroup
431450
}, token).ConfigureAwait(false);
@@ -436,7 +455,7 @@ private async Task<string> LogEventTransmissionSetup(CancellationToken token)
436455

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

455474
try
456475
{
457-
var streamResponse = await _client.CreateLogStreamAsync(new CreateLogStreamRequest
476+
var streamResponse = await _client.Value.CreateLogStreamAsync(new CreateLogStreamRequest
458477
{
459478
LogGroupName = _config.LogGroup,
460479
LogStreamName = currentStreamName
@@ -486,7 +505,7 @@ private void PutRetentionPolicy(int logGroupRetentionInDays, string logGroup, st
486505
{
487506
try
488507
{
489-
var putPolicyResponse = await _client.PutRetentionPolicyAsync(new PutRetentionPolicyRequest(logGroup, logGroupRetentionInDays), token).ConfigureAwait(false);
508+
var putPolicyResponse = await _client.Value.PutRetentionPolicyAsync(new PutRetentionPolicyRequest(logGroup, logGroupRetentionInDays), token).ConfigureAwait(false);
490509
if (!IsSuccessStatusCode(putPolicyResponse))
491510
{
492511
LogLibraryServiceError(new System.Net.WebException($"Put retention policy {logGroupRetentionInDays} for LogGroup {logGroup} returned status: {putPolicyResponse.HttpStatusCode}"), serviceURL);

src/AWS.Logger.Core/IAWSLoggerConfig.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public interface IAWSLoggerConfig
142142
bool LibraryLogErrors { get; set; }
143143

144144
/// <summary>
145-
/// 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.
145+
/// 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.
146146
/// <para>
147147
/// The default is going to "aws-logger-errors.txt".
148148
/// </para>

src/AWS.Logger.Log4net/AWS.Logger.Log4net.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<SignAssembly>True</SignAssembly>
2424
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
25-
<Version>3.5.2</Version>
25+
<Version>3.5.3</Version>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

src/AWS.Logger.Log4net/AWSAppender.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public TimeSpan FlushTimeout
233233
}
234234

235235
/// <summary>
236-
/// 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.
236+
/// 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.
237237
/// <para>
238238
/// The default is going to "aws-logger-errors.txt".
239239
/// </para>

src/AWS.Logger.SeriLog/AWS.Logger.SeriLog.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<SignAssembly>True</SignAssembly>
2424
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
25-
<Version>3.4.2</Version>
25+
<Version>3.4.3</Version>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

src/NLog.AWS.Logger/AWSTarget.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public bool LibraryLogErrors
223223
}
224224

225225
/// <summary>
226-
/// 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.
226+
/// 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.
227227
/// <para>
228228
/// The default is "aws-logger-errors.txt".
229229
/// </para>

src/NLog.AWS.Logger/NLog.AWS.Logger.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<SignAssembly>True</SignAssembly>
2525
<AssemblyOriginatorKeyFile>..\..\public.snk</AssemblyOriginatorKeyFile>
26-
<Version>3.3.3</Version>
26+
<Version>3.3.4</Version>
2727
</PropertyGroup>
2828

2929
<ItemGroup>

0 commit comments

Comments
 (0)