Instantly share code, notes, and snippets.

Embed
What would you like to do?
.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