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
1 change: 1 addition & 0 deletions eng/PatchConfig.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(VersionPrefix)' == '2.2.2' ">
<PackagesInPatch>
Microsoft.Extensions.Logging.AzureAppServices
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

</PackagesInPatch>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract class BatchingLoggerProvider: ILoggerProvider
private readonly int? _batchSize;
private readonly IDisposable _optionsChangeToken;

private int _messagesDropped;

private BlockingCollection<LogMessage> _messageQueue;
private Task _outputTask;
private CancellationTokenSource _cancellationTokenSource;
Expand Down Expand Up @@ -78,6 +80,16 @@ private async Task ProcessLogQueue(object state)
limit--;
}

var messagesDropped = Interlocked.Exchange(ref _messagesDropped, 0);
if (messagesDropped != 0)
{
_currentBatch.Add(new LogMessage()
{
Message = $"{messagesDropped} message(s) dropped because of queue size limit. Increase the queue size or decrease logging verbosity to avoid this.{Environment.NewLine}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about referencing BatchingLoggerOptions in this message or including a URL for the docs?

Timestamp = DateTimeOffset.Now
});
}

if (_currentBatch.Count > 0)
{
try
Expand All @@ -91,7 +103,6 @@ private async Task ProcessLogQueue(object state)

_currentBatch.Clear();
}

await IntervalAsync(_interval, _cancellationTokenSource.Token);
}
}
Expand All @@ -107,7 +118,10 @@ internal void AddMessage(DateTimeOffset timestamp, string message)
{
try
{
_messageQueue.Add(new LogMessage { Message = message, Timestamp = timestamp }, _cancellationTokenSource.Token);
if (!_messageQueue.TryAdd(new LogMessage { Message = message, Timestamp = timestamp }, millisecondsTimeout: 0, cancellationToken: _cancellationTokenSource.Token))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the millisecondsTimeout: 0 needed for this change?

Copy link
Author

@pakrym pakrym Jan 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
Interlocked.Increment(ref _messagesDropped);
}
}
catch
{
Expand Down Expand Up @@ -160,4 +174,4 @@ public ILogger CreateLogger(string categoryName)
return new BatchingLogger(this, categoryName);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -51,7 +51,6 @@ public async Task RespectsBatchSize()
Assert.Single(provider.Batches);
Assert.Single(provider.Batches[0]);
Assert.Equal("2016-05-04 03:02:01.000 +00:00 [Information] Cat: Info message" + _nl, provider.Batches[0][0].Message);

provider.IntervalControl.Resume();
await provider.IntervalControl.Pause;

Expand All @@ -62,22 +61,22 @@ public async Task RespectsBatchSize()
}

[Fact]
public async Task BlocksWhenReachingMaxQueue()
public async Task DropsMessagesWhenReachingMaxQueue()
{
var provider = new TestBatchingLoggingProvider(maxQueueSize: 1);
var logger = (BatchingLogger)provider.CreateLogger("Cat");

await provider.IntervalControl.Pause;

logger.Log(_timestampOne, LogLevel.Information, 0, "Info message", null, (state, ex) => state);
var task = Task.Run(() => logger.Log(_timestampOne.AddHours(1), LogLevel.Error, 0, "Error message", null, (state, ex) => state));

Assert.False(task.Wait(1000));
logger.Log(_timestampOne.AddHours(1), LogLevel.Error, 0, "Error message", null, (state, ex) => state);

provider.IntervalControl.Resume();
await provider.IntervalControl.Pause;

Assert.True(task.Wait(1000));
Assert.Equal(2, provider.Batches[0].Length);
Assert.Equal("2016-05-04 03:02:01.000 +00:00 [Information] Cat: Info message" + _nl, provider.Batches[0][0].Message);
Assert.Equal("1 message(s) dropped because of queue size limit. Increase the queue size or decrease logging verbosity to avoid this." + _nl, provider.Batches[0][1].Message);
}

private class TestBatchingLoggingProvider: BatchingLoggerProvider
Expand Down