Skip to content

Commit 7b8d9de

Browse files
committed
Added log flush timer capability
1 parent aea3a45 commit 7b8d9de

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

dotNet/CoreHelpers.TaskLogging.Sample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
1111

1212
// add the azure storage table logger
13-
builder.Services.AddTaskLoggerForAzureStorageTable("UseDevelopmentStorage=true", "Dev", 100);
13+
builder.Services.AddTaskLoggerForAzureStorageTable("UseDevelopmentStorage=true", "Dev", 100, TimeSpan.FromSeconds(30));
1414

1515
// register the task logger framework
1616
builder.Services.AddLogging(

dotNet/CoreHelpers.TaskLogging.Sample/Worker.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Microsoft.Extensions.Logging;
33
using CoreHelpers.Extensions.Logging.Tasks;
4+
using Microsoft.Extensions.Hosting;
45

56
namespace CoreHelpers.TaskLogging.Sample
67
{
@@ -9,13 +10,15 @@ internal class Worker
910
private readonly ILogger<Worker> _logger;
1011
private readonly IEnumerable<IProcessor> _processors;
1112
private readonly ITaskLoggerFactory _taskLoggerFactory;
12-
13-
public Worker(ILogger<Worker> logger, IEnumerable<IProcessor> processors, ITaskLoggerFactory taskLoggerFactory)
13+
private readonly IHostApplicationLifetime _appLifetime;
14+
15+
public Worker(ILogger<Worker> logger, IEnumerable<IProcessor> processors, ITaskLoggerFactory taskLoggerFactory, IHostApplicationLifetime appLifetime)
1416
{
1517
_logger = logger;
1618
_processors = processors;
1719
_taskLoggerFactory = taskLoggerFactory;
18-
}
20+
_appLifetime = appLifetime;
21+
}
1922

2023
public async Task Process()
2124
{
@@ -45,6 +48,36 @@ public async Task Process()
4548
{
4649
await _processors.Where(p => p is ProcessorSuccess).First().Execute();
4750
}
51+
52+
// execute the success processor
53+
using (var typedLoggerTaskScope = _logger.BeginNewTaskScope("successjob", "q", "w"))
54+
{
55+
// in the scope of the task a shutdown handler will be registered
56+
_appLifetime.ApplicationStopping.Register(() =>
57+
{
58+
_logger.LogError("We are shutting down the application");
59+
if (typedLoggerTaskScope != null)
60+
typedLoggerTaskScope.Dispose();
61+
62+
});
63+
64+
// log something
65+
_logger.LogInformation("Will be logged in the timespan");
66+
await Task.Delay(TimeSpan.FromSeconds(60));
67+
68+
// log something
69+
_logger.LogInformation(("Logging something we should see after graceful shurtdown"));
70+
71+
// trigger a graceful shutdown
72+
_appLifetime.StopApplication();
73+
74+
// prevent the application code to leave the scope
75+
await Task.Delay(TimeSpan.FromHours(1));
76+
77+
// log something we never see again
78+
_logger.LogInformation(("This should never be shown"));
79+
}
80+
4881
}
4982
}
5083
}

dotNet/CoreHelpers.TaskLogging/AzureStorageTableTaskLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ internal class AzureStorageTableTaskLogger : BaseTaskLogger
88
{
99
private AzureStorageTableTaskLoggerFactory _loggerFactory;
1010

11-
public AzureStorageTableTaskLogger(string taskId, int cacheLimit, AzureStorageTableTaskLoggerFactory loggerFactory)
12-
: base(taskId, cacheLimit)
11+
public AzureStorageTableTaskLogger(string taskId, int cacheLimit, TimeSpan cacheTimespan, AzureStorageTableTaskLoggerFactory loggerFactory)
12+
: base(taskId, cacheLimit, cacheTimespan)
1313
{
1414
_loggerFactory = loggerFactory;
1515
}

dotNet/CoreHelpers.TaskLogging/AzureStorageTableTaskLoggerFactory.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ namespace CoreHelpers.TaskLogging
1111
{
1212
internal class AzureStorageTableTaskLoggerFactory : ITaskLoggerFactory
1313
{
14-
private int _cacheLimit;
15-
private string _environmentPrefix;
16-
private TableServiceClient _tableServiceClient;
14+
private readonly int _cacheLimit;
15+
private readonly TimeSpan _cacheTimespan;
16+
private readonly string _environmentPrefix;
17+
private readonly TableServiceClient _tableServiceClient;
1718
private long _messageTimeStampCounter = 0;
1819

19-
public AzureStorageTableTaskLoggerFactory(string connectionString, string environmentPrefix, int cacheLimit)
20+
public AzureStorageTableTaskLoggerFactory(string connectionString, string environmentPrefix, int cacheLimit, TimeSpan cacheTimespan)
2021
{
2122
_tableServiceClient = new TableServiceClient(connectionString);
2223
_environmentPrefix = environmentPrefix;
23-
_cacheLimit = cacheLimit;
24+
_cacheLimit = cacheLimit;
25+
_cacheTimespan = cacheTimespan;
2426
}
2527

2628
public async Task<string> AnnounceTask(string taskType, string taskSource, string taskWorker)
@@ -99,7 +101,7 @@ public async Task UpdateTaskStatus(string taskKey, TaskStatus taskStatus)
99101

100102
public ITaskLogger CreateTaskLogger(string taskKey)
101103
{
102-
return new AzureStorageTableTaskLogger(taskKey, _cacheLimit, this);
104+
return new AzureStorageTableTaskLogger(taskKey, _cacheLimit, _cacheTimespan, this);
103105
}
104106

105107
public async Task Flush(DateTimeOffset flushTime, string taskKey, IEnumerable<string> messages)
@@ -207,7 +209,13 @@ public static class AzureStorageTableTaskLoggerFactoryServiceCollectionExtension
207209
{
208210
public static IServiceCollection AddTaskLoggerForAzureStorageTable(this IServiceCollection services, string connectionString, string environmentPrefix, int lineCacheLimit)
209211
{
210-
services.AddSingleton<ITaskLoggerFactory>(new AzureStorageTableTaskLoggerFactory(connectionString, environmentPrefix, lineCacheLimit));
212+
services.AddSingleton<ITaskLoggerFactory>(new AzureStorageTableTaskLoggerFactory(connectionString, environmentPrefix, lineCacheLimit, TimeSpan.FromMinutes(5)));
213+
return services;
214+
}
215+
216+
public static IServiceCollection AddTaskLoggerForAzureStorageTable(this IServiceCollection services, string connectionString, string environmentPrefix, int lineCacheLimit, TimeSpan cacheTimeSpan)
217+
{
218+
services.AddSingleton<ITaskLoggerFactory>(new AzureStorageTableTaskLoggerFactory(connectionString, environmentPrefix, lineCacheLimit, cacheTimeSpan));
211219
return services;
212220
}
213221
}

dotNet/CoreHelpers.TaskLogging/BaseTaskLogger.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Newtonsoft.Json;
56

@@ -8,13 +9,19 @@ namespace CoreHelpers.TaskLogging
89
internal abstract class BaseTaskLogger : ITaskLogger
910
{
1011
private int _cacheLimit;
12+
private readonly TimeSpan _cacheTimeSpan;
1113
private string _taskId;
1214
private List<string> _messages = new List<string>();
13-
14-
public BaseTaskLogger(string taskId, int cacheLimit)
15+
private readonly Timer _flushTimer;
16+
17+
public BaseTaskLogger(string taskId, int cacheLimit, TimeSpan cacheTimeSpan)
1518
{
1619
_taskId = taskId;
1720
_cacheLimit = cacheLimit;
21+
_cacheTimeSpan = cacheTimeSpan;
22+
_flushTimer = new Timer((state) => {
23+
FlushInternal();
24+
}, null, _cacheTimeSpan, _cacheTimeSpan);
1825
}
1926

2027
public void Log(TaskLogLevel logLevel, string message, Exception? exception)
@@ -38,6 +45,7 @@ public void Log(TaskLogLevel logLevel, string message, Exception? exception, Fun
3845
_messages.Add(formatter(message, exception));
3946
}
4047

48+
4149
// flush if needed
4250
if (_messages.Count >= _cacheLimit)
4351
FlushInternal();
@@ -47,8 +55,9 @@ public void Log(TaskLogLevel logLevel, string message, Exception? exception, Fun
4755
public void Dispose()
4856
{
4957
FlushInternal();
58+
_flushTimer.Dispose();
5059
}
51-
60+
5261
private void FlushInternal()
5362
{
5463
lock(_messages)

0 commit comments

Comments
 (0)