Program.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 
  2. using log4net;
  3. using SCADA.CommonLib.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Topshelf;
  10. namespace SCADA_DAQ.Service
  11. {
  12. class ProgramMain
  13. {
  14. static void Main(string[] args)
  15. {
  16. ILog log = SCADA.CommonLib.LoggerHelper.Logger.CreatLogger(typeof(ProgramMain));
  17. AppDomain.CurrentDomain.UnhandledException += (s, e) =>
  18. {
  19. log.Error(e);
  20. };
  21. Env.StartTick = Environment.TickCount;
  22. HostFactory.Run(x =>
  23. {
  24. var cfg = x.Service<ServiceHost>(
  25. s =>
  26. {
  27. s.ConstructUsing(name => ServiceHost.Instance);
  28. s.WhenStarted(t => t.Start());
  29. s.WhenPaused(t => t.Paused());
  30. s.WhenContinued(t => t.Resume());
  31. s.WhenStopped(t => t.Stop());
  32. });
  33. //x.UseLog4Net("log4net.config");
  34. x.SetDescription("SCADA 采集");
  35. x.SetDisplayName($"SCADA 采集 {ApplicationHelper.GetAppVersion()}");
  36. x.SetInstanceName("SCADA_Service");
  37. x.StartAutomatically(); // Automatic (Delayed) -- only available on .NET 4.0 or later
  38. x.RunAsLocalSystem();
  39. x.EnableServiceRecovery((e) =>
  40. {
  41. e.RestartService(0);//第一次失败执行
  42. e.RestartService(0);//第二次失败执行
  43. e.RestartService(0);//后续失败执行
  44. e.OnCrashOnly();
  45. e.SetResetPeriod(1);
  46. });
  47. x.OnException(ex =>
  48. {
  49. log.Error(ex); //异常处理
  50. })
  51. ;
  52. });
  53. }
  54. }
  55. }