Created
January 21, 2019 04:40
-
-
Save guitarrapc/31d58318160192f5ad9aba6bd56fbaa0 to your computer and use it in GitHub Desktop.
.NET Core 2.1 GenericHost Sample
This file contains 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
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