Skip to content

Commit

Permalink
fix: Fix logging by flushing the app insights buffer (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
killij authored Jan 19, 2024
1 parent a6c9639 commit 0413a78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
14 changes: 13 additions & 1 deletion src/Childrens-Social-Care-CPD-Indexer.Tests/WorkerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Childrens_Social_Care_CPD_Indexer.Core;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NSubstitute.ExceptionExtensions;
Expand All @@ -11,6 +14,7 @@ public class WorkerTests
private IApplicationConfiguration _config;
private IResourcesIndexer _indexer;
private IHostApplicationLifetime _hostingApplicationLifetime;
private TelemetryClient _telemetryClient;
private Worker _sut;

[SetUp]
Expand All @@ -21,7 +25,15 @@ public void Setup()
_indexer = Substitute.For<IResourcesIndexer>();
_hostingApplicationLifetime = Substitute.For<IHostApplicationLifetime>();

_sut = new Worker(_logger, _indexer, _config, _hostingApplicationLifetime);
var configuration = new TelemetryConfiguration();
var sendItems = new List<ITelemetry>();
var channel = Substitute.For<ITelemetryChannel>();
configuration.TelemetryChannel = channel;
configuration.ConnectionString = "InstrumentationKey={Guid.NewGuid()};";
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
_telemetryClient = new TelemetryClient(configuration);

_sut = new Worker(_logger, _indexer, _config, _hostingApplicationLifetime, _telemetryClient);
}

[TearDown]
Expand Down
34 changes: 11 additions & 23 deletions src/Childrens-Social-Care-CPD-Indexer/Worker.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
using Childrens_Social_Care_CPD_Indexer.Core;
using Microsoft.ApplicationInsights;

namespace Childrens_Social_Care_CPD_Indexer;

public class Worker : BackgroundService
public class Worker(ILogger<Worker> logger, IResourcesIndexer resourcesIndexer, IApplicationConfiguration applicationConfiguration, IHostApplicationLifetime hostApplicationLifetime, TelemetryClient telemetryClient) : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IResourcesIndexer _resourcesIndexer;
private readonly IApplicationConfiguration _config;
private readonly IHostApplicationLifetime _hostApplicationLifetime;

public Worker(ILogger<Worker> logger, IResourcesIndexer resourcesIndexer, IApplicationConfiguration applicationConfiguration, IHostApplicationLifetime hostApplicationLifetime)
{
_logger = logger;
_resourcesIndexer = resourcesIndexer;
_hostApplicationLifetime = hostApplicationLifetime;
_config = applicationConfiguration;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken) =>
await DoWork(stoppingToken).ContinueWith(task => _hostApplicationLifetime.StopApplication(), stoppingToken);
protected override async Task ExecuteAsync(CancellationToken stoppingToken) => await DoWork(stoppingToken).ContinueWith(task => hostApplicationLifetime.StopApplication(), stoppingToken);

private async Task DoWork(CancellationToken stoppingToken)
{
_logger.LogInformation("Indexing started at: {startTime}", DateTime.Now);
logger.LogInformation("Indexing started at: {startTime}", DateTime.Now);
try
{
if (_config.SearchIndexing.RecreateIndex)
if (applicationConfiguration.SearchIndexing.RecreateIndex)
{
await _resourcesIndexer.DeleteIndexAsync(_config.SearchIndexing.IndexName, stoppingToken);
await resourcesIndexer.DeleteIndexAsync(applicationConfiguration.SearchIndexing.IndexName, stoppingToken);
}
await _resourcesIndexer.CreateIndexAsync(_config.SearchIndexing.IndexName, stoppingToken);
await _resourcesIndexer.PopulateIndexAsync(_config.SearchIndexing.IndexName, _config.SearchIndexing.BatchSize, stoppingToken);
await resourcesIndexer.CreateIndexAsync(applicationConfiguration.SearchIndexing.IndexName, stoppingToken);
await resourcesIndexer.PopulateIndexAsync(applicationConfiguration.SearchIndexing.IndexName, applicationConfiguration.SearchIndexing.BatchSize, stoppingToken);

}
catch (Exception ex)
{
_logger.LogError(ex, "Unhandled exception occured");
logger.LogError(ex, "Unhandled exception occured");
}
finally
{
_logger.LogInformation("Indexing finished at: {finishTime}", DateTime.Now);
logger.LogInformation("Indexing finished at: {finishTime}", DateTime.Now);
await telemetryClient.FlushAsync(stoppingToken);
}
}
}

0 comments on commit 0413a78

Please sign in to comment.