Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created January 21, 2019 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/31d58318160192f5ad9aba6bd56fbaa0 to your computer and use it in GitHub Desktop.
Save guitarrapc/31d58318160192f5ad9aba6bd56fbaa0 to your computer and use it in GitHub Desktop.
.NET Core 2.1 GenericHost Sample
class Program
{
static async Task Main(string[] args)
{
await new HostBuilder()
.ConfigureAppConfiguration((hostContext, configApp) =>
{
// Configの追加
hostContext.HostingEnvironment.EnvironmentName = System.Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT") ?? "production";
configApp.SetBasePath(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));
configApp.AddCommandLine(args);
configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json");
})
.ConfigureServices(services =>
{
// サービス処理のDI
services.AddSingleton<IFooService, FooService>();
services.AddSingleton<IBarService, BarService>();
})
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
// Console ロガーの追加
b.AddConsole();
// NLog や Log4Net、SeriLog などを追加
// あるいはApplication Insight の追加
})
.RunConsoleAsync()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment