Program.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. Env.ParameterInit();
  23. HostFactory.Run(x =>
  24. {
  25. var cfg = x.Service<ServiceHost>(
  26. s =>
  27. {
  28. s.ConstructUsing(name => ServiceHost.Instance);
  29. s.WhenStarted(t => t.Start());
  30. s.WhenPaused(t => t.Paused());
  31. s.WhenContinued(t => t.Resume());
  32. s.WhenStopped(t => t.Stop());
  33. });
  34. //x.UseLog4Net("log4net.config");
  35. x.SetDescription("SCADA 采集");
  36. x.SetDisplayName($"SCADA 采集 {ApplicationHelper.GetAppVersion()}");
  37. x.SetInstanceName("SCADA_Service");
  38. x.StartAutomatically(); // Automatic (Delayed) -- only available on .NET 4.0 or later
  39. x.RunAsLocalSystem();
  40. x.EnableServiceRecovery((e) =>
  41. {
  42. e.RestartService(0);//第一次失败执行
  43. e.RestartService(0);//第二次失败执行
  44. e.RestartService(0);//后续失败执行
  45. e.OnCrashOnly();
  46. e.SetResetPeriod(1);
  47. });
  48. x.OnException(ex =>
  49. {
  50. log.Error(ex); //异常处理
  51. })
  52. ;
  53. });
  54. }
  55. }
  56. }