Skip to content

Commit

Permalink
Merge pull request #12 from serilog/dev
Browse files Browse the repository at this point in the history
2.1.0 Release
  • Loading branch information
nblumhardt authored Oct 25, 2016
2 parents c43f9dc + d914f53 commit d22f3d7
Show file tree
Hide file tree
Showing 23 changed files with 907 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,5 @@ _Pvt_Extensions

# FAKE - F# Make
.fake/

BenchmarkDotNet.Artifacts/
16 changes: 16 additions & 0 deletions RunPerfTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Push-Location $PSScriptRoot

./Build.ps1

foreach ($test in ls test/*.PerformanceTests) {
Push-Location $test

echo "perf: Running performance test project in $test"

& dotnet test -c Release
if($LASTEXITCODE -ne 0) { exit 2 }

Pop-Location
}

Pop-Location
9 changes: 8 additions & 1 deletion serilog-sinks-periodicbatching.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
EndProject
Expand All @@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F6E07A13-B
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Sinks.PeriodicBatching.Tests", "test\Serilog.Sinks.PeriodicBatching.Tests\Serilog.Sinks.PeriodicBatching.Tests.xproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Serilog.Sinks.PeriodicBatching.PerformanceTests", "test\Serilog.Sinks.PeriodicBatching.PerformanceTests\Serilog.Sinks.PeriodicBatching.PerformanceTests.xproj", "{80B760D1-3862-49AD-9D72-23608550C318}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,12 +37,17 @@ Global
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.Build.0 = Release|Any CPU
{80B760D1-3862-49AD-9D72-23608550C318}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80B760D1-3862-49AD-9D72-23608550C318}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80B760D1-3862-49AD-9D72-23608550C318}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80B760D1-3862-49AD-9D72-23608550C318}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{324C2F52-D9F7-4844-9BC4-9906E228D380} = {037440DE-440B-4129-9F7A-09B42D00397E}
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {F6E07A13-B9D3-4019-B25A-DE1F6C17E108}
{80B760D1-3862-49AD-9D72-23608550C318} = {F6E07A13-B9D3-4019-B25A-DE1F6C17E108}
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions src/Serilog.Sinks.PeriodicBatching/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]

[assembly: InternalsVisibleTo("Serilog.Sinks.PeriodicBatching.PerformanceTests, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" +
"6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" +
"d18dbf6d5a25af5ce9016f281014d79dc3b4201ac646c451830fc7e61a2dfd633d34c39f87b818" +
"94191652df5ac63cc40c77f3542f702bda692e6e8a9158353df189007a49da0f3cfd55eb250066" +
"b19485ec")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Concurrent;
using System.Threading;

namespace Serilog.Sinks.PeriodicBatching
{
class BoundedConcurrentQueue<T>
{
const int NON_BOUNDED = -1;

readonly ConcurrentQueue<T> _queue = new ConcurrentQueue<T>();
readonly int _queueLimit;

int _counter;

public BoundedConcurrentQueue()
{
_queueLimit = NON_BOUNDED;
}

public BoundedConcurrentQueue(int queueLimit)
{
if (queueLimit <= 0)
throw new ArgumentOutOfRangeException(nameof(queueLimit), "queue limit must be positive");

_queueLimit = queueLimit;
}

public int Count => _queue.Count;

public bool TryDequeue(out T item)
{
if (_queueLimit == NON_BOUNDED)
return _queue.TryDequeue(out item);

var result = false;
try
{ }
finally // prevent state corrupt while aborting
{
if (_queue.TryDequeue(out item))
{
Interlocked.Decrement(ref _counter);
result = true;
}
}

return result;
}

public bool TryEnqueue(T item)
{
if (_queueLimit == NON_BOUNDED)
{
_queue.Enqueue(item);
return true;
}

var result = true;
try
{ }
finally
{
if (Interlocked.Increment(ref _counter) <= _queueLimit)
{
_queue.Enqueue(item);
}
else
{
Interlocked.Decrement(ref _counter);
result = false;
}
}

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ namespace Serilog.Sinks.PeriodicBatching
public abstract class PeriodicBatchingSink : ILogEventSink, IDisposable
{
readonly int _batchSizeLimit;
readonly ConcurrentQueue<LogEvent> _queue;
readonly BoundedConcurrentQueue<LogEvent> _queue;
readonly BatchedConnectionStatus _status;
readonly Queue<LogEvent> _waitingBatch = new Queue<LogEvent>();

readonly object _stateLock = new object();
#if WAITABLE_TIMER
readonly Timer _timer;
#else

readonly PortableTimer _timer;
#endif

bool _unloading;
bool _started;

Expand All @@ -58,16 +56,24 @@ public abstract class PeriodicBatchingSink : ILogEventSink, IDisposable
protected PeriodicBatchingSink(int batchSizeLimit, TimeSpan period)
{
_batchSizeLimit = batchSizeLimit;
_queue = new ConcurrentQueue<LogEvent>();
_queue = new BoundedConcurrentQueue<LogEvent>();
_status = new BatchedConnectionStatus(period);

#if WAITABLE_TIMER
_timer = new Timer(s => OnTick(), null, -1, -1);
#else

_timer = new PortableTimer(cancel => OnTick());
#endif
}


/// <summary>
/// Construct a sink posting to the specified database.
/// </summary>
/// <param name="batchSizeLimit">The maximum number of events to include in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="queueLimit">Maximum number of events in the queue.</param>
protected PeriodicBatchingSink(int batchSizeLimit, TimeSpan period, int queueLimit)
: this(batchSizeLimit, period)
{
_queue = new BoundedConcurrentQueue<LogEvent>(queueLimit);
}

void CloseAndFlush()
{
lock (_stateLock)
Expand All @@ -77,14 +83,8 @@ void CloseAndFlush()

_unloading = true;
}

#if WAITABLE_TIMER
var wh = new ManualResetEvent(false);
if (_timer.Dispose(wh))
wh.WaitOne();
#else

_timer.Dispose();
#endif

OnTick();
}
Expand Down Expand Up @@ -198,12 +198,7 @@ void OnTick()

void SetTimer(TimeSpan interval)
{
#if WAITABLE_TIMER
_timer.Change(interval, Timeout.InfiniteTimeSpan);
#else
_timer.Start(interval);
#endif

}

/// <summary>
Expand Down Expand Up @@ -233,15 +228,15 @@ public void Emit(LogEvent logEvent)
{
// Special handling to try to get the first event across as quickly
// as possible to show we're alive!
_queue.Enqueue(logEvent);
_queue.TryEnqueue(logEvent);
_started = true;
SetTimer(TimeSpan.Zero);
return;
}
}
}

_queue.Enqueue(logEvent);
_queue.TryEnqueue(logEvent);
}

/// <summary>
Expand Down
Loading

0 comments on commit d22f3d7

Please sign in to comment.