Skip to content

Commit 73f5c59

Browse files
author
mwatson
committed
Ability to flush metrics queue on shutdown. NLog collect all params, ability to configure that.
1 parent f89b80e commit 73f5c59

File tree

11 files changed

+91
-26
lines changed

11 files changed

+91
-26
lines changed
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
512 Bytes
Binary file not shown.

Dlls/StackifyLib/StackifyLib.dll

512 Bytes
Binary file not shown.

Src/StackifyLib.log4net.v1_2_10/StackifyAppender.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class StackifyAppender : AppenderSkeleton
3030
public string threadContextKeys { get; set; }
3131
public string logicalThreadContextKeys { get; set; }
3232
public string callContextKeys { get; set; }
33-
public bool logMethodNames { get; set; }
33+
public bool? logMethodNames { get; set; }
3434

3535
private List<string> _GlobalContextKeys = new List<string>();
3636
private List<string> _ThreadContextKeys = new List<string>();
@@ -46,6 +46,8 @@ protected override void OnClose()
4646
try
4747
{
4848
_logClient.Close();
49+
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue();
50+
4951
}
5052
catch
5153
{
@@ -148,7 +150,7 @@ internal LogMsg Translate(LoggingEvent loggingEvent)
148150
{
149151
msg.SrcMethod = loggingEvent.LoggerName;
150152

151-
if (logMethodNames)
153+
if (logMethodNames ?? false)
152154
{
153155
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);
154156

Src/StackifyLib.log4net/StackifyAppender.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class StackifyAppender : AppenderSkeleton
2121
public string threadContextKeys { get; set; }
2222
public string logicalThreadContextKeys { get; set; }
2323
public string callContextKeys { get; set; }
24-
public bool logMethodNames { get; set; }
24+
public bool? logMethodNames { get; set; }
2525

2626
private List<string> _GlobalContextKeys = new List<string>();
2727
private List<string> _ThreadContextKeys = new List<string>();
@@ -37,6 +37,8 @@ protected override void OnClose()
3737
try
3838
{
3939
_logClient.Close();
40+
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue();
41+
4042
}
4143
catch
4244
{
@@ -139,7 +141,7 @@ internal LogMsg Translate(LoggingEvent loggingEvent)
139141
{
140142
msg.SrcMethod = loggingEvent.LoggerName;
141143

142-
if (logMethodNames)
144+
if (logMethodNames ?? false)
143145
{
144146
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);
145147

Src/StackifyLib.nLog/StackifyTarget.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class StackifyTarget : TargetWithLayout
2828
public string globalContextKeys { get; set; }
2929
public string mappedContextKeys { get; set; }
3030
public string callContextKeys { get; set; }
31-
public bool logMethodNames { get; set; }
31+
public bool? logMethodNames { get; set; }
32+
public bool? logAllParams { get; set; }
3233

3334
private List<string> _GlobalContextKeys = new List<string>();
3435
private List<string> _MappedContextKeys = new List<string>();
@@ -38,8 +39,15 @@ public class StackifyTarget : TargetWithLayout
3839

3940
protected override void CloseTarget()
4041
{
41-
_logClient.Close();
42-
42+
try
43+
{
44+
_logClient.Close();
45+
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue();
46+
47+
}
48+
catch
49+
{
50+
}
4351
}
4452

4553
protected override void InitializeTarget()
@@ -193,7 +201,7 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
193201
{
194202
msg.SrcMethod = loggingEvent.LoggerName;
195203

196-
if (logMethodNames)
204+
if ((logMethodNames ?? false))
197205
{
198206
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);
199207

@@ -255,6 +263,17 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
255263
{
256264
debugObject = null;
257265
}
266+
else if (logAllParams ?? true)
267+
{
268+
Dictionary<string, object> args = new Dictionary<string, object>();
269+
270+
for (int i = 0; i < loggingEvent.Parameters.Length; i++)
271+
{
272+
args["arg" + i] = loggingEvent.Parameters[i];
273+
}
274+
275+
debugObject = args;
276+
}
258277
}
259278

260279
if (debugObject != null)

Src/StackifyLib/Internal/Logs/LogClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public void Close()
103103
{
104104
if (_LogQueue != null)
105105
{
106+
//stop will flush the queue
106107
_LogQueue.Stop();
107108
}
108109
}

Src/StackifyLib/Internal/Metrics/MetricClient.cs

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace StackifyLib.Internal.Metrics
1212
{
13-
internal static class MetricClient
13+
public static class MetricClient
1414
{
1515
private static HttpClient _httpClient = new HttpClient(null, null);
1616

@@ -23,6 +23,7 @@ internal static class MetricClient
2323
private static ConcurrentDictionary<string, MetricAggregate> _LastAggregates = new ConcurrentDictionary<string, MetricAggregate>();
2424
private static ConcurrentDictionary<string, MetricSetting> _MetricSettings = new ConcurrentDictionary<string, MetricSetting>();
2525

26+
private static bool _StopRequested = false;
2627

2728
static MetricClient()
2829
{
@@ -271,10 +272,49 @@ private static void UploadMetricsCheck(object state)
271272
StackifyAPILogger.Log("Upload metrics check");
272273

273274
_Timer.Change(-1, -1);
274-
bool allSuccess = false;
275-
DateTime purgeOlderThan = DateTime.UtcNow.AddMinutes(-10);
276-
DateTime currentMinute = DateTime.UtcNow.Floor(TimeSpan.FromMinutes(1));
277-
List<KeyValuePair<string, MetricAggregate>> metrics = new List<KeyValuePair<string, MetricAggregate>>();
275+
276+
double seconds = 15;
277+
278+
if (!_StopRequested)
279+
{
280+
bool allSuccess = false;
281+
DateTime purgeOlderThan = DateTime.UtcNow.AddMinutes(-10);
282+
283+
DateTime currentMinute = DateTime.UtcNow.Floor(TimeSpan.FromMinutes(1));
284+
285+
allSuccess = UploadMetrics(currentMinute);
286+
287+
PurgeOldMetrics(purgeOlderThan);
288+
289+
if (_AggregateMetrics.Count > 0 && allSuccess)
290+
{
291+
seconds = .1;
292+
}
293+
}
294+
295+
296+
297+
_Timer.Change(TimeSpan.FromSeconds(seconds), TimeSpan.FromSeconds(seconds));
298+
299+
}
300+
301+
public static void StopMetricsQueue()
302+
{
303+
//don't let t his method run more than once
304+
if (_StopRequested)
305+
return;
306+
307+
_StopRequested = true;
308+
309+
DateTime currentMinute = DateTime.UtcNow.AddMinutes(2).Floor(TimeSpan.FromMinutes(1));
310+
311+
UploadMetrics(currentMinute);
312+
}
313+
314+
public static bool UploadMetrics(DateTime currentMinute)
315+
{
316+
bool success = false;
317+
List<KeyValuePair<string, MetricAggregate>> metrics = new List<KeyValuePair<string, MetricAggregate>>();
278318
try
279319
{
280320
//read everything up to the start of the current minute
@@ -317,7 +357,7 @@ private static void UploadMetricsCheck(object state)
317357

318358
//only getting metrics less than 10 minutes old to drop old data in case we get backed up
319359
//they are removed from the _AggregateMetrics in the upload function upon success
320-
allSuccess =
360+
success =
321361
UploadAggregates(
322362
metrics.Where(x => x.Value.OccurredUtc > DateTime.UtcNow.AddMinutes(-10)).ToList());
323363

@@ -332,7 +372,7 @@ private static void UploadMetricsCheck(object state)
332372
}
333373
catch (Exception ex)
334374
{
335-
allSuccess = false;
375+
success = false;
336376
StackifyAPILogger.Log("Error uploading metrics " + ex.ToString());
337377

338378
//if an error put them back in
@@ -347,14 +387,20 @@ private static void UploadMetricsCheck(object state)
347387
}
348388
}
349389

390+
return success;
391+
392+
}
393+
394+
private static void PurgeOldMetrics(DateTime purgeOlderThan)
395+
{
350396
try
351397
{
352398
//beginning of this method we save off latest aggregates before upload or purge
353399

354400
//purge old stuff
355401
//if uploading disabled it purges everything
356402
//if success uploading then should be nothing to purge as that is already done above
357-
403+
358404
var oldMetrics = _AggregateMetrics.Where(x => x.Value.OccurredUtc < purgeOlderThan).ToList();
359405
MetricAggregate oldval;
360406
oldMetrics.ForEach(x => _AggregateMetrics.TryRemove(x.Key, out oldval));
@@ -365,16 +411,6 @@ private static void UploadMetricsCheck(object state)
365411
StackifyAPILogger.Log("Error purging metrics " + ex.ToString());
366412
}
367413

368-
369-
double seconds = 15;
370-
371-
if (_AggregateMetrics.Count > 0 && allSuccess)
372-
{
373-
seconds = .1;
374-
}
375-
376-
_Timer.Change(TimeSpan.FromSeconds(seconds), TimeSpan.FromSeconds(seconds));
377-
378414
}
379415

380416
private static void SetLatestAggregates(List<MetricAggregate> aggregates)

0 commit comments

Comments
 (0)