前几天把控制台项目弄成系统服务启动,但是出现上图的错误,服务一下子就停止运行了。
一开始以为是端口被占用,结果不是。
①、出现这种错误一般是代码有问题,可以使用try catch 打印日志。
我当时问题是无法打印日志,原因是加载日志配置文件路径出错:
static void Main(string[] args){// 错误log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("../../log4net.config"));//正确log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(getLogPath());ServiceBase[] ServicesToRun;ServicesToRun = new ServiceBase[]{new shortConnection()};ServiceBase.Run(ServicesToRun);}/// <summary>/// 日志路径/// </summary>/// <returns></returns>private static string getLogPath(){string path = System.AppDomain.CurrentDomain.BaseDirectory;string rootpath = path.Substring(0, path.LastIndexOf("\\"));rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\"));rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\")) + "\\log4net.config";return rootpath;}
②、查看window 事件日志。
如果日志没打印错误,就通过 控制面板->系统和安全->查看事件日志->windows 日志,
查看系统日志。
当时的问题是.net framework 4.5.2 框架不匹配,通过下载指定框架版本即可。
通过这两种思路可以解决上述问题。