Skip to content

Commit 4077ec0

Browse files
committed
Added portable time from Seq
1 parent ba41f93 commit 4077ec0

File tree

7 files changed

+144
-9
lines changed

7 files changed

+144
-9
lines changed

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ internal class EventCollectorRequest : HttpRequestMessage
4646
{
4747
internal EventCollectorRequest(string splunkHost, string jsonPayLoad)
4848
{
49-
5049
var stringContent = new StringContent(jsonPayLoad, Encoding.UTF8, "application/json");
5150
RequestUri = new Uri(splunkHost);
5251
Content = stringContent;

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable
4242
private readonly SplunkJsonFormatter _jsonFormatter;
4343
private readonly ConcurrentQueue<LogEvent> _queue;
4444
private readonly EventCollectorClient _httpClient;
45+
private readonly PortableTimer _timer;
4546

4647
/// <summary>
4748
/// Taken from Splunk.Logging.Common
@@ -53,6 +54,7 @@ public class EventCollectorSink : ILogEventSink, IDisposable
5354
HttpStatusCode.BadRequest
5455
};
5556

57+
5658
/// <summary>
5759
/// Creates a new instance of the sink
5860
/// </summary>
@@ -80,9 +82,13 @@ public EventCollectorSink(
8082
var batchInterval = TimeSpan.FromSeconds(batchIntervalInSeconds);
8183

8284
_httpClient = new EventCollectorClient(_eventCollectorToken);
83-
85+
8486
var cancellationToken = new CancellationToken();
8587

88+
// _timer = new PortableTimer(async c => await ProcessQueue());
89+
90+
91+
8692
RepeatAction.OnInterval(
8793
batchInterval,
8894
async () => await ProcessQueue(),
@@ -181,8 +187,8 @@ private async Task Send(IEnumerable<LogEvent> events)
181187

182188
allEvents = $"{allEvents}{splunkEvent.Payload}";
183189
}
184-
var request = new EventCollectorRequest(_splunkHost, allEvents);
185190

191+
var request = new EventCollectorRequest(_splunkHost, allEvents);
186192
var response = await _httpClient.SendAsync(request);
187193

188194
if (response.IsSuccessStatusCode)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2013-2015 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if !TIMER
16+
using Serilog.Debugging;
17+
using System;
18+
using System.Threading;
19+
using System.Threading.Tasks;
20+
21+
namespace Serilog.Sinks.Splunk
22+
{
23+
class PortableTimer : IDisposable
24+
{
25+
enum PortableTimerState
26+
{
27+
NotWaiting,
28+
Waiting,
29+
Active,
30+
Disposed
31+
}
32+
33+
readonly object _stateLock = new object();
34+
PortableTimerState _state = PortableTimerState.NotWaiting;
35+
36+
readonly Action<CancellationToken> _onTick;
37+
readonly CancellationTokenSource _cancel = new CancellationTokenSource();
38+
39+
public PortableTimer(Action<CancellationToken> onTick)
40+
{
41+
if (onTick == null) throw new ArgumentNullException(nameof(onTick));
42+
_onTick = onTick;
43+
}
44+
45+
public async void Start(TimeSpan interval)
46+
{
47+
if (interval < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(interval));
48+
49+
lock (_stateLock)
50+
{
51+
if (_state == PortableTimerState.Disposed)
52+
throw new ObjectDisposedException("PortableTimer");
53+
54+
// There's a little bit of raciness here, but it's needed to support the
55+
// current API, which allows the tick handler to reenter and set the next interval.
56+
57+
if (_state == PortableTimerState.Waiting)
58+
throw new InvalidOperationException("The timer is already set.");
59+
60+
if (_cancel.IsCancellationRequested) return;
61+
62+
_state = PortableTimerState.Waiting;
63+
}
64+
65+
try
66+
{
67+
await Task.Delay(interval, _cancel.Token).ConfigureAwait(false);
68+
69+
_state = PortableTimerState.Active;
70+
71+
if (!_cancel.Token.IsCancellationRequested)
72+
{
73+
_onTick(_cancel.Token);
74+
}
75+
}
76+
catch (TaskCanceledException tcx)
77+
{
78+
SelfLog.WriteLine("The timer was canceled during invocation: {0}", tcx);
79+
}
80+
finally
81+
{
82+
lock (_stateLock)
83+
_state = PortableTimerState.NotWaiting;
84+
}
85+
}
86+
87+
public void Dispose()
88+
{
89+
_cancel.Cancel();
90+
91+
while (true)
92+
{
93+
lock (_stateLock)
94+
{
95+
if (_state == PortableTimerState.Disposed ||
96+
_state == PortableTimerState.NotWaiting)
97+
{
98+
_state = PortableTimerState.Disposed;
99+
return;
100+
}
101+
}
102+
103+
Thread.Sleep(10);
104+
}
105+
}
106+
}
107+
}
108+
#endif

src/Serilog.Sinks.Splunk/project.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
},
1212
"frameworks": {
1313
"net45": {
14+
"compilationOptions": {
15+
"define": ["TCP", "UDP"]
16+
},
1417
"dependencies": {
1518
"System.Net.Http": "2.0.20710"
1619
}

src/sample/Program.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
3+
using System.Net;
24
using System.Threading;
35
using Serilog;
46
using Serilog.Core;
7+
using Serilog.Sinks.Splunk;
58

69
namespace Sample
710
{
811
public class Program
912
{
1013
public static void Main(string[] args)
11-
{
14+
{
15+
string splunkHost = "https://192.168.71.1:8088";
16+
string splunkEventCollectorToken = "274AD921-FB85-429B-B09E-4EE069843218";
17+
1218

1319
Log.Logger = new LoggerConfiguration()
14-
.WriteTo.LiterateConsole()
20+
.WriteTo.LiterateConsole()
21+
.WriteTo.EventCollector (splunkHost, splunkEventCollectorToken)
1522
.CreateLogger();
1623

24+
Serilog.Debugging.SelfLog.Out = Console.Out;
25+
1726
Log.Information("Sample starting up");
1827

1928
foreach (var i in Enumerable.Range(0, 1000))
@@ -23,6 +32,8 @@ public static void Main(string[] args)
2332
Thread.Sleep(1000);
2433
Log.Debug("Loop iteration done");
2534
}
35+
36+
Console.ReadLine();
2637
}
2738
}
2839
}

src/sample/project.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
},
1717

1818
"commands": {
19-
"Sample": "Sample"
19+
"Sample": "run"
2020
},
2121

2222
"frameworks": {
23-
"dnx451": { },
23+
"dnx451": {
24+
"dependencies": {
25+
},
26+
"frameworkAssemblies": {
27+
"System.Net": "4.0.0.0"
28+
}
29+
},
2430
"dnxcore50": {
2531
"dependencies": {
2632
"Microsoft.CSharp": "4.0.1-beta-23516",

src/sample/project.lock.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4220,7 +4220,9 @@
42204220
"Serilog.Sinks.Literate >= 2.0.0-beta-21",
42214221
"Serilog.Sinks.Splunk "
42224222
],
4223-
"DNX,Version=v4.5.1": [],
4223+
"DNX,Version=v4.5.1": [
4224+
"fx/System.Net >= 4.0.0"
4225+
],
42244226
"DNXCore,Version=v5.0": [
42254227
"Microsoft.CSharp >= 4.0.1-beta-23516",
42264228
"System.Collections >= 4.0.11-beta-23516",

0 commit comments

Comments
 (0)