#pragma warning disable IDE1006 // Naming Styles public static async Task Main(string[] args) #pragma warning restore IDE1006 // Naming Styles { Console.WriteLine($"BUILD-TIME\n{new string('=', 20)}\n{await File.ReadAllTextAsync(@"Resources/BuildInfo.txt")}\nRUN-TIME\n{new string('=', 20)}\n{Assembly.GetExecutingAssembly().GetName().Version}\nDOTNET_ENVIRONMENT: {Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")}\nDOTNET_RUNNING_IN_CONTAINER: {Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER")}\nMACHINENAME: {Environment.MachineName}"); #if (ConsoleApplication) bool isConsole = args.Contains("--console"); if (isConsole) { var unhandledExceptionHelper = new UnhandledExceptionHelper(); unhandledExceptionHelper.Register(); //x unhandledExceptionHelper.SetLogger(Startup.Instance.ServiceProvider.GetService<ILoggerFactory>()); //x unhandledExceptionHelper.LogExistingCrashes(true); await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { #if (BackgroundService) services.AddGracePeriodManagerService(hostContext.Configuration); #endif services.AddHelper(hostContext.Configuration); }) .RunConsoleAsync(); unhandledExceptionHelper.Unregister(); return; } #endif IHostBuilder hostBuilder = CreateHostBuilder(args); #if (WindowsService) bool isService = !Debugger.IsAttached && args.Contains("--windowsService"); if (isService) { hostBuilder.UseWindowsService(); } #endif IHost host = hostBuilder.Build(); ILogger <Program> logger = host.Services.GetService <ILogger <Program> >(); IWebHostEnvironment hostingEnvironment = host.Services.GetService <IWebHostEnvironment>(); AssemblyName assemblyName = Assembly.GetExecutingAssembly().GetName(); var applicationGuid = Guid.NewGuid(); using (logger.ProgramScope(assemblyName.Name , assemblyName.Version.ToString() , applicationGuid.ToString())) { logger.ProgramStarted(Process.GetCurrentProcess().Id , Thread.CurrentThread.ManagedThreadId); logger.ProgramInitial(hostingEnvironment.EnvironmentName , EnvironmentHelper.IsDocker , Environment.UserInteractive , Debugger.IsAttached , false // delete this , args.ToArray()); Initialize(host.Services); await host.RunAsync(); logger.ProgramStopping(Process.GetCurrentProcess() .Id , Thread.CurrentThread.ManagedThreadId); } }
public static async Task Main(string[] args) { var unhandledExceptionHelper = new UnhandledExceptionHelper(); bool enableUnhandledExceptionHelper = !Debugger.IsAttached; if (enableUnhandledExceptionHelper) { unhandledExceptionHelper.Register(); //x unhandledExceptionHelper.SetLogger(Startup.Instance.ServiceProvider.GetService<ILoggerFactory>()); //x unhandledExceptionHelper.LogExistingCrashes(true); } using (var mutex = new Mutex(false , Assembly.GetExecutingAssembly() .GetName() .Name)) { const int mutexTimeout = 2; if (!mutex.WaitOne(TimeSpan.FromSeconds(mutexTimeout) , false)) { const string message = "Another instance is running."; Debug.WriteLine(message); return; } IHost host = BuildHost(args); var logger = host.Services.GetService <ILogger <Program> >(); AssemblyName assemblyName = Assembly.GetExecutingAssembly() .GetName(); string assemblyInfo = $"{assemblyName.Name} v{assemblyName.Version}"; Guid applicationGuid = Guid.NewGuid(); //using (logger.BeginScope("{ApplicationName} :: {ApplicationVersion} :: {ApplicationGuid}", assemblyName.Name, assemblyName.Version, applicationGuid)) //logger.LogInformation($"{new string('-', 19)} Start {nameof(RunProgram)} Environment.UserInteractive {{Environment_UserInteractive}}, Debugger.IsAttached {{Debugger_IsAttached}}, {{ProcessId}}, {{ThreadId}}, Arguments {{Args}}", Environment.UserInteractive, Debugger.IsAttached, Process.GetCurrentProcess().Id, Thread.CurrentThread.ManagedThreadId, args); //logger.LogInformation($"{new string('-', 19)} Stop {nameof(RunProgram)} {{ProcessId}}, {{ThreadId}}", Process.GetCurrentProcess().Id, Thread.CurrentThread.ManagedThreadId); using (logger.ProgramScope(assemblyInfo , applicationGuid.ToString())) { logger.ProgramStarted(EnvironmentHelper.EnvironmentName , Environment.UserInteractive , Debugger.IsAttached , Process.GetCurrentProcess() .Id , Thread.CurrentThread.ManagedThreadId , args.ToArray()); Initialize(host.Services); await host.RunAsync(); logger.ProgramStopping(Process.GetCurrentProcess() .Id , Thread.CurrentThread.ManagedThreadId); } } if (enableUnhandledExceptionHelper) { unhandledExceptionHelper.Unregister(); } }