working cli with dummy hosted service (logging, config)

This commit is contained in:
andy 2021-10-11 01:06:43 +01:00
parent 507861bc28
commit ab8687b41e
5 changed files with 112 additions and 30 deletions

View File

@ -1,42 +1,34 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
namespace Selector.CLI
{
class Program
{
static void Main(string[] args)
public static async Task Main(string[] args)
{
var serviceProvider = new ServiceCollection()
//.AddLogging(b =>
// b.AddFilter("Microsoft", LogLevel.Warning)
// .AddFilter("System", LogLevel.Warning)
// .AddFilter("Selector.CLI.Program", LogLevel.Debug)
// .AddConsole()
//)
.AddTransient<IPlayerWatcher, PlayerWatcher>()
.AddTransient<IWatcherCollection, WatcherCollection>()
.BuildServiceProvider();
//using var loggerFactory = LoggerFactory.Create(builder =>
//{
// builder
// .AddFilter("Microsoft", LogLevel.Warning)
// .AddFilter("System", LogLevel.Warning)
// .AddFilter("Selector.CLI.Program", LogLevel.Debug)
// .AddConsole();
//});
//ILogger logger = loggerFactory.CreateLogger<Program>();
//logger.LogInformation("Example log message");
//var logger = serviceProvider.GetService<ILoggerFactory>()
// .CreateLogger<Program>();
//logger.LogDebug("Starting application");
var logger = serviceProvider.GetService<ILogger>();
logger.LogDebug("All done!");
var host = CreateHostBuilder(args);
await host.RunConsoleAsync();
}
static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) => {
services.AddTransient<IPlayerWatcher, PlayerWatcher>();
services.AddTransient<IWatcherCollection, WatcherCollection>();
services.AddHostedService<WatcherService>();
})
.ConfigureLogging(builder => {
builder
.AddSimpleConsole(options => {
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "yyyy-mm-dd hh:mm:ss ";
});
});
}
}

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="SpotifyAPI.Web" Version="6.2.2" />
@ -18,4 +19,10 @@
<ProjectReference Include="..\Selector\Selector.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Selector.CLI
{
class SelectorOptions
{
public const string Options = "Selector";
public int Number { get; set; }
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Selector.CLI
{
class WatcherService : IHostedService
{
private readonly ILogger<WatcherService> Logger;
private readonly IConfiguration Config;
public WatcherService(ILogger<WatcherService> logger, IConfiguration config)
{
Logger = logger;
Config = config;
}
public Task StartAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("Starting up");
foreach ((var key, var pair) in Config.AsEnumerable())
//foreach ((var key, var pair) in Config.GetSection("Selector").AsEnumerable())
{
Logger.LogInformation($"{key} => {pair}");
}
using(Logger.BeginScope("A New Scope!"))
{
Logger.LogError("From the scope!");
}
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("Shutting down");
return Task.CompletedTask;
}
}
}

View File

@ -0,0 +1,20 @@
{
"string config": "asdf",
"Selector": {
"Number": 4
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"Console": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
//"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}