using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Security { internal class Program { static log4net.ILog log; static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; log4net.Config.XmlConfigurator.Configure(); log = log4net.LogManager.GetLogger("loginfo"); var is_debug = ConfigurationManager.AppSettings["is_debug"]; if (is_debug == "1") { var service = new ServiceSecurity(); Thread t = new Thread(delegate () { service.DebugOnStart(args); }); t.Start(); Console.ReadLine(); } else { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new ServiceSecurity() }; ServiceBase.Run(ServicesToRun); } } private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { log.Info("time:" + DateTime.Now.ToString() + ";error:" + (e.ExceptionObject as Exception).ToString());//写入一条新log } } }