Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix logging by flushing the app insights buffer #26

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}