Skip to content

Commit 42b08d9

Browse files
authored
Merge pull request #25 from stackify/feature/SF-6393-Millisecond-Ordering
SF-6393 Millisecond Ordering
2 parents 75208be + ac92920 commit 42b08d9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Src/StackifyLib/Internal/Logs/LogClient.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Net;
66
using System.Text;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
//using System.Web.Configuration;
910
using Newtonsoft.Json;
@@ -123,6 +124,11 @@ public bool ErrorShouldBeSent(StackifyError error)
123124
return governor.ErrorShouldBeSent(error);
124125
}
125126

127+
128+
129+
private static long _lastEpochMs = 0;
130+
private static int _millisecondCount = 0;
131+
126132
public void QueueMessage(LogMsg msg)
127133
{
128134
if (msg == null) return;
@@ -139,6 +145,21 @@ public void QueueMessage(LogMsg msg)
139145
isError = 1;
140146
}
141147

148+
// works on the assumption that the epochMS will always be incrementing as it reaches this point
149+
if (_lastEpochMs < msg.EpochMs)
150+
{
151+
// reset counter if we are no longer in the same ms
152+
//https://msdn.microsoft.com/en-us/library/system.threading.interlocked_methods(v=vs.110).aspx
153+
Interlocked.Exchange(ref _lastEpochMs, msg.EpochMs);
154+
Interlocked.Exchange(ref _millisecondCount, 0);
155+
msg.Order = 0;
156+
}
157+
else if (_lastEpochMs == msg.EpochMs)
158+
{
159+
msg.Order = Interlocked.Increment(ref _millisecondCount);
160+
}
161+
// else defaulted to 0
162+
142163
//Used by Stackify profiler only
143164
if (Logger.PrefixEnabled())
144165
{

Src/StackifyLib/Models/LogMsgGroup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public LogMsg()
9595
[JsonProperty]
9696
public List<string> Tags { get; set; }
9797

98+
[JsonProperty]
99+
public int Order { get; set; }
100+
98101
[JsonIgnore]
99102
public LogMsgGroup AppDetails { get; set; }
100103

0 commit comments

Comments
 (0)