static async Task Main() { ConfigManager configManager; DataOptions dataOptions; DataBaseWorker appInsights; try { if (File.Exists(xmlPath) && File.Exists(xsdPath)) { XmlSchemaSet schema = new XmlSchemaSet(); schema.Add(string.Empty, xsdPath); XDocument xdoc = XDocument.Load(xmlPath); xdoc.Validate(schema, ValidationEventHandler); configManager = new ConfigManager(xmlPath, typeof(DataOptions)); } else if (File.Exists(jsonPath)) { configManager = new ConfigManager(jsonPath, typeof(DataOptions)); } else { throw new Exception("No options"); } dataOptions = configManager.GetOptions <DataOptions>(); appInsights = new DataBaseWorker(dataOptions.LoggerConnectionString); await appInsights.ClearInsightsAsync(); await appInsights.InsertInsightAsync("Connection established"); } catch (Exception ex) { using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exceptions.txt"), true)) { await sw.WriteLineAsync($"{DateTime.Now:dd/MM/yyyy HH:mm:ss} Exception: {ex.Message}"); } return; } try { DataManager service = new DataManager(dataOptions, appInsights); ServiceBase.Run(service); } catch (Exception ex) { await appInsights.InsertInsightAsync("Exception: " + ex.Message); await appInsights.WriteInsightsToXmlAsync(dataOptions.OutputFolder); } }
protected override void OnStop() { await appInsights.InsertInsightAsync("Stop"); await appInsights.WriteInsightsToXmlAsync(dataOptions.OutputFolder); }