generate a error eventlog if everything fails (#283)

This commit is contained in:
Peter Hsu 2019-08-12 14:29:43 -07:00 коммит произвёл GitHub
Родитель 65f79292ed
Коммит f03620a9eb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 76 добавлений и 57 удалений

Просмотреть файл

@ -11,69 +11,88 @@ namespace Microsoft.IIS.Administration {
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;
using Serilog;
using Serilog;
using System;
using System.Diagnostics;
public class Program {
public const string EventSourceName = "Microsoft IIS Administration API";
public static void Main(string[] args) {
//
// Build Config
var configHelper = new ConfigurationHelper(args);
IConfiguration config = configHelper.Build();
//
// Initialize runAsAService local variable
string serviceName = config.GetValue<string>("serviceName")?.Trim();
bool runAsAService = !string.IsNullOrEmpty(serviceName);
//
// Host
using (var host = new WebHostBuilder()
.UseContentRoot(configHelper.RootPath)
.ConfigureLogging((hostingContext, logging) => {
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
try
{
//
// Build Config
var configHelper = new ConfigurationHelper(args);
IConfiguration config = configHelper.Build();
//
// Console log is not available in running as a Service
if (!runAsAService) {
logging.AddConsole();
//
// Initialize runAsAService local variable
string serviceName = config.GetValue<string>("serviceName")?.Trim();
bool runAsAService = !string.IsNullOrEmpty(serviceName);
//
// Host
using (var host = new WebHostBuilder()
.UseContentRoot(configHelper.RootPath)
.ConfigureLogging((hostingContext, logging) => {
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
//
// Console log is not available in running as a Service
if (!runAsAService)
{
logging.AddConsole();
}
logging.AddDebug();
logging.AddEventLog(new EventLogSettings()
{
SourceName = EventSourceName
});
})
.UseUrls("https://*:55539") // Config can override it. Use "urls":"https://*:55539"
.UseConfiguration(config)
.ConfigureServices(s => s.AddSingleton(config)) // Configuration Service
.UseStartup<Startup>()
.UseHttpSys(o => {
//
// Kernel mode Windows Authentication
o.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;
//
// Need anonymous to allow CORS preflight requests
// app.UseWindowsAuthentication ensures (if needed) the request is authenticated to proceed
o.Authentication.AllowAnonymous = true;
})
.Build()
.UseHttps())
{
if (runAsAService)
{
//
// Run as a Service
Log.Information($"Running as service: {serviceName}");
host.RunAsService();
}
logging.AddDebug();
logging.AddEventLog(new EventLogSettings() {
SourceName = EventSourceName
});
})
.UseUrls("https://*:55539") // Config can override it. Use "urls":"https://*:55539"
.UseConfiguration(config)
.ConfigureServices(s => s.AddSingleton(config)) // Configuration Service
.UseStartup<Startup>()
.UseHttpSys(o => {
//
// Kernel mode Windows Authentication
o.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;
//
// Need anonymous to allow CORS preflight requests
// app.UseWindowsAuthentication ensures (if needed) the request is authenticated to proceed
o.Authentication.AllowAnonymous = true;
})
.Build()
.UseHttps()) {
if (runAsAService) {
//
// Run as a Service
Log.Information($"Running as service: {serviceName}");
host.RunAsService();
}
else {
//
// Run interactive
host.Run();
}
else
{
//
// Run interactive
host.Run();
}
}
}
catch (Exception ex)
{
using (var shutdownLog = new EventLog("Application"))
{
shutdownLog.Source = Program.EventSourceName;
shutdownLog.WriteEntry($"Microsoft IIS Administration API has shutdown unexpectively because the error: {ex.ToString()}", EventLogEntryType.Error);
}
throw ex;
}
}
}
}
}