Created
September 24, 2017 01:08
-
-
Save gravity00/672fc6706c231ea1bfefd7e49b582847 to your computer and use it in GitHub Desktop.
SimpleSoft.Hosting.WithBuilderAndStartup
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
| public static class Program | |
| { | |
| private static readonly CancellationTokenSource TokenSource; | |
| static Program() | |
| { | |
| TokenSource = new CancellationTokenSource(); | |
| Console.CancelKeyPress += (sender, args) => | |
| { | |
| TokenSource.Cancel(); | |
| args.Cancel = true; | |
| }; | |
| } | |
| public static int Main(string[] args) => | |
| MainAsync(args, TokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult(); | |
| private static async Task<int> MainAsync(string[] args, CancellationToken ct) | |
| { | |
| int resultCode; | |
| var loggerFactory = new LoggerFactory().AddConsole(LogLevel.Trace, true); | |
| var logger = loggerFactory.CreateLogger(typeof(Program)); | |
| logger.LogInformation("Application started..."); | |
| try | |
| { | |
| using (var hostBuilder = new HostBuilder("ENVIRONMENT") | |
| .UseLoggerFactory(loggerFactory) | |
| .UseStartup(new Startup(args))) | |
| { | |
| await hostBuilder.RunHostAsync<Application>(ct).ConfigureAwait(false); | |
| } | |
| resultCode = 0; | |
| } | |
| catch (TaskCanceledException) | |
| { | |
| logger.LogWarning("The application execution was canceled by request"); | |
| resultCode = 2; | |
| } | |
| catch (Exception e) | |
| { | |
| logger.LogCritical(0, e, "Unexpected exception has occured"); | |
| resultCode = 1; | |
| } | |
| logger.LogInformation("Application terminated. Press <enter> to exit..."); | |
| Console.ReadLine(); | |
| return resultCode; | |
| } | |
| public class Startup : HostStartup { | |
| private readonly string[] _args; | |
| public Startup(string[] args) | |
| { | |
| _args = args; | |
| } | |
| public override void ConfigureConfigurationBuilder(IConfigurationBuilderParam param) | |
| { | |
| param.Builder | |
| .SetBasePath(param.Environment.ContentRootPath) | |
| .AddJsonFile("appsettings.json", false, true) | |
| .AddJsonFile($"appsettings.{param.Environment.Name}.json", true, true) | |
| .AddEnvironmentVariables() | |
| .AddCommandLine(_args); | |
| } | |
| public override void ConfigureLoggerFactory(ILoggerFactoryHandlerParam param) | |
| { | |
| param.LoggerFactory | |
| .AddNLog() | |
| .ConfigureNLog( | |
| param.Environment.ContentRootFileProvider.GetFileInfo("nlog.config").PhysicalPath); | |
| } | |
| } | |
| public class Application : IHost | |
| { | |
| private readonly ILogger<Application> _logger; | |
| public Application(ILogger<Application> logger) | |
| { | |
| _logger = logger; | |
| } | |
| public Task RunAsync(CancellationToken ct) | |
| { | |
| _logger.LogInformation("Running the application logic"); | |
| return Task.CompletedTask; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment