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

View File

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