/// <summary> /// 在服务启动时执行 /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { try { TaskTimer.Elapsed += TaskTimer_Elapsed; TaskTimer.Start(); } catch (Exception ex) { WinLogger.LogEvent(ex.Message); } }
/// <summary> /// 当服务关闭时执行 /// </summary> protected override void OnStop() { IUnityContainer container = UnityHelper.GetUnityContainer; foreach (var service in container.ResolveAll <ITask>()) { try { service.Stop(); } catch (Exception ex) { Logger.Error(ex.Message); WinLogger.LogEvent(ex.Message); } } }
/// <summary> /// 每秒钟执行的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void TaskTimer_Elapsed(object sender, ElapsedEventArgs e) { IUnityContainer container = UnityHelper.GetUnityContainer; foreach (var service in container.ResolveAll <ITask>()) { try { service.Start(); } catch (Exception ex) { Logger.Error(ex.Message); WinLogger.LogEvent(ex.Message); } } }
public Tweet Parse(string stringValue) { ILogger <string> logger = new WinLogger(); (double, double)coordinates = (0, 0); DbGeography dbCoordinates = null; DateTime dateTime = new DateTime(); string value = ""; try { Regex regex = new Regex(@"\[(?<coordinate1>\-?\d+\.\d+),\s(?<coordinate2>\-?\d+\.\d+)\]\s_\s(?<year>[0-9]{4})\-(?<month>[0-9]{2})\-(?<day>[0-9]{2})\s(?<hour>[0-9]{2})\:(?<minute>[0-9]{2})\:(?<second>[0-9]{2})\s(?<text>[\w\W]+)"); Match match = regex.Match(stringValue); coordinates.Item1 = double.Parse(match.Groups["coordinate1"].Value, CultureInfo.InvariantCulture); coordinates.Item2 = double.Parse(match.Groups["coordinate2"].Value, CultureInfo.InvariantCulture); dbCoordinates = DbGeography.PointFromText(string.Format($"POINT(" + coordinates.Item2.ToString(CultureInfo.InvariantCulture) + " " + coordinates.Item1.ToString(CultureInfo.InvariantCulture) + ")"), 4326); dateTime = new DateTime( int.Parse(match.Groups["year"].Value), int.Parse(match.Groups["month"].Value), int.Parse(match.Groups["day"].Value), int.Parse(match.Groups["hour"].Value), int.Parse(match.Groups["minute"].Value), int.Parse(match.Groups["second"].Value)); value = match.Groups["text"].Value; } catch (Exception) { logger.Log($"String{Environment.NewLine}'{stringValue}'{Environment.NewLine}doesn't correspond parse form"); Environment.Exit(0); } Tweet tweet = new Tweet(dbCoordinates, dateTime, value); return(tweet); }
public CommonMail(ExchangeService ewsClient, string mailboxName, ItemId emailItemId) { try { Logger = new WinLogger("ExchangeLog", "Ms Exchange"); Email = EmailMessage.Bind(ewsClient, emailItemId, new PropertySet( BasePropertySet.FirstClassProperties, ItemSchema.Categories, ItemSchema.DateTimeReceived, ItemSchema.DateTimeSent, EmailMessageSchema.CcRecipients, EmailMessageSchema.ToRecipients, ItemSchema.Subject, ItemSchema.MimeContent, ItemSchema.TextBody, ItemSchema.Attachments)); ThisMailbox = mailboxName; } catch { return; } }
public MsExchange(Action <ItemId> mailProcessingMethod) { Logger = new WinLogger("ExchangeLog", "Ms Exchange"); MailProcessingMethod = mailProcessingMethod; }
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { _logger.FatalException("Unhandled Exception", e.Exception); WinLogger.DumpLogsToFile().ConfigureAwait(false); e.Handled = true; }
public ContextInitializer() { ILogger <string> logger = new WinLogger(); Context = new DataContext(); string sentimentsFilename = ""; string polygonsFilename = ""; try { Context.MapSource = ConfigurationManager.AppSettings["map"]; using (var reader = new StreamReader(ConfigurationManager.AppSettings["sentiments"])) { using (var csvReader = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = false, HeaderValidated = null, })) { var records = csvReader.GetRecords <Tuple <string, float> >(); foreach (var record in records) { Context.Sentiments.Add(record.Item1, record.Item2); } } } } catch (System.Configuration.ConfigurationException) { logger.Log($"Error in Configuration file"); Environment.Exit(0); } catch (FileNotFoundException) { logger.Log($"File {sentimentsFilename} wasn't found"); Environment.Exit(0); } catch (Exception) { logger.Log($"{sentimentsFilename} file value doesn't correspond parse form"); Environment.Exit(0); } try { polygonsFilename = ConfigurationManager.AppSettings["states"]; string text = File.ReadAllText(polygonsFilename); char[] spaces = new char[] { ' ', '\t', '\n', '\r' }; int index; do { index = text.IndexOfAny(spaces); if (index != -1) { text = text.Remove(index, 1); } } while (index != -1); Regex regex = new Regex("(?=\"(?<postalCode>[A-Z]{2})\":(?<polygons>\\[.+?\\])(,\"|\\}))"); List <Match> matches = regex.Matches(text).Cast <Match>().ToList(); foreach (var match in matches) { string tempString = match.Groups["polygons"].Value; tempString = tempString.Replace("[[[", "[["); tempString = tempString.Replace("]]]", "]]"); IList <IList <IList <float> > > polygonsCoordinates = new List <IList <IList <float> > >(); if (tempString[2] == '[') { polygonsCoordinates = JsonConvert.DeserializeObject <List <IList <IList <float> > > >(tempString); } else { polygonsCoordinates.Add(JsonConvert.DeserializeObject <List <IList <float> > >(tempString)); } IList <DbGeography> polygons = new List <DbGeography>(); IList <IList <PointF> > polygonsPoints = new List <IList <PointF> >(); foreach (var polygon in polygonsCoordinates) { polygons.Add(GeographyConverter.ConvertCoordinatesToPolygon(polygon)); polygonsPoints.Add(GeographyConverter.ConvertCoordinatesToPoints(polygon)); } Context.StatesPolygons.Add(match.Groups["postalCode"].Value, polygons); Context.StatesPolygonsPoints.Add(match.Groups["postalCode"].Value, polygonsPoints); } } catch (System.Configuration.ConfigurationException) { logger.Log($"Error in Configuration file"); Environment.Exit(0); } catch (FileNotFoundException) { logger.Log($"File {polygonsFilename} wasn't found"); Environment.Exit(0); } catch (Exception) { logger.Log($"File {polygonsFilename} doesn't correspond parse form"); Environment.Exit(0); } }