Created
November 21, 2020 16:17
-
-
Save mastersign/c5973745cdbdbbab8e5cc87cecce705a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using Microsoft.Extensions.Hosting; | |
| using Microsoft.Extensions.Logging; | |
| namespace Mastersign.Playground.Hosted.Cli | |
| { | |
| public class CliService : IHostedService | |
| { | |
| private ILogger Log { get; } | |
| private IHostApplicationLifetime Lifetime { get; } | |
| public CliService( | |
| ILogger<CliService> log, | |
| IHostApplicationLifetime lifetime) | |
| { | |
| Log = log; | |
| Lifetime = lifetime; | |
| } | |
| public Task StartAsync(CancellationToken cancellationToken) | |
| { | |
| Log.LogInformation("CLI Service started"); | |
| Lifetime.StopApplication(); | |
| return Task.CompletedTask; | |
| } | |
| public Task StopAsync(CancellationToken cancellationToken) | |
| { | |
| Log.LogInformation("CLI Service stopping"); | |
| return Task.CompletedTask; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project Sdk="Microsoft.NET.Sdk"> | |
| <PropertyGroup> | |
| <TargetFramework>net5.0</TargetFramework> | |
| <OutputType>Exe</OutputType> | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> | |
| <PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" /> | |
| <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> | |
| </ItemGroup> | |
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Hosting; | |
| using Serilog; | |
| namespace Mastersign.Playground.Hosted.Cli | |
| { | |
| public static class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Console.WriteLine($"Starting Generic Host..."); | |
| var logTemplate = | |
| "{Timestamp:MMdd HH:mm:ss} [{Level:u3}] {SourceContext}{NewLine}" + | |
| " {Message:lj}{NewLine}{Exception}"; | |
| Log.Logger = new LoggerConfiguration() | |
| .MinimumLevel.Verbose() | |
| .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Debug) | |
| .Enrich.FromLogContext() | |
| .WriteTo.Console(outputTemplate: logTemplate) | |
| .CreateLogger(); | |
| CreateHostBuilder(args).Build().Run(); | |
| } | |
| static IHostBuilder CreateHostBuilder(string[] args) | |
| { | |
| return Host.CreateDefaultBuilder(args) | |
| .ConfigureServices((hostContext, services) => | |
| { | |
| services.AddHostedService<CliService>(); | |
| }) | |
| .UseSerilog(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment