Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Last active November 29, 2018 08:22
Show Gist options
  • Save PureKrome/06421fa7170ab2ef348a0dd1be52d10b to your computer and use it in GitHub Desktop.
Save PureKrome/06421fa7170ab2ef348a0dd1be52d10b to your computer and use it in GitHub Desktop.
Playing around with the new .NET Generic Host in .NET Core 2.1+
//
// REF: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.1
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1#add-providers
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1#memory-configuration-provider
//
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Notifications.Products.BackgroundTasks
{
public class Program
{
private static readonly Dictionary<string, string> appSettingsData =
new Dictionary<string, string>
{
{"Logging:LogLevel:Default", "Debug"},
{"Logging:Console:IncludeScopes", "true"},
};
private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.AddInMemoryCollection(appSettingsData)
.Build();
public static async Task Main(string[] args)
{
var loggingProvider = new ConsoleLoggerProvider((category, logLevel) => logLevel >= LogLevel.Debug, true);
var logger = loggingProvider.CreateLogger(nameof(Program));
try
{
var host = new HostBuilder()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(Configuration);
logging.AddConsole();
})
.ConfigureServices((hostingContext, services) =>
{
services.AddHostedService<TestBackgroundService1>();
services.AddHostedService<TestBackgroundService2>();
})
.Build();
await host.RunAsync();
}
catch (Exception exception)
{
// TODO: Track exception to A.I., here.
logger.LogError(exception, "Stopped program because of an exception.");
}
logger.LogDebug("Finished shutting down app.");
Console.ReadKey();
}
}
public abstract class TestBackgroundService : BackgroundService
{
private readonly ILogger<TestBackgroundService> _logger;
protected TestBackgroundService(ILogger<TestBackgroundService> logger)
{
_logger = logger;
}
public abstract string Name { get; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
for (var i = 0; i < 5; i++)
{
await DoStuffAsync($"Stuff {Name} - {i}", stoppingToken);
}
}
private async Task DoStuffAsync(string text, CancellationToken cancellationToken)
{
_logger.LogInformation("About to do stuff - " + text);
await Task.Delay(2 * 1000, cancellationToken);
_logger.LogInformation("Finished doing stuff - " + text);
}
}
public class TestBackgroundService1 : TestBackgroundService
{
public TestBackgroundService1(ILogger<TestBackgroundService> logger) : base(logger)
{
}
public override string Name => "1";
}
public class TestBackgroundService2 : TestBackgroundService
{
public TestBackgroundService2(ILogger<TestBackgroundService> logger) : base(logger)
{
}
public override string Name => "2";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment