第一步:新建控制台项目
第二步:添加服务
第三步:右键新建完成的服务项
点击
在start 和stop事件中分别写上
第四步 编写代码
双击打开
1 using System; 2 using System.Diagnostics; 3 using System.IO; 4 using System.Linq; 5 using System.ServiceProcess; 6 7 namespace SocketService 8 { 9 class Program 10 { 11 #region 可以修改的参数 12 //指定服务名称 13 private static string ServiceName = "SocketService"; 14 //更换服务显示名称 15 private static string DisplayServiceName = "SocketService"; 16 //更换服务拓展名 17 private static string Extension = ".exe"; 18 //更改此项即可更换Service 19 private static ServiceBase ServiceToRun = new YSocketService(); 20 #endregion 21 static void Main(string[] args) 22 { 23 if (args.Length > 0 && args[0] == "s") 24 { 25 ServiceBase[] ServicesToRun; 26 ServicesToRun = new ServiceBase[] 27 { 28 ServiceToRun, 29 }; 30 ServiceBase.Run(ServicesToRun); 31 } 32 else 33 { 34 Console.WriteLine("通用Service半自动化服务操作启动......"); 35 bool isExit = false; 36 while (!isExit) 37 { 38 Console.WriteLine("请选择,[1]安装服务 [2]启动服务 [3]停止服务 [4]卸载服务 [5]退出"); 39 bool IsServiceInstalled = ISWindowsServiceInstalled(ServiceName); 40 var rs = Convert.ToInt32(Console.ReadLine()); 41 switch (rs) 42 { 43 case 1: 44 //取当前可执行文件路径 45 var path = Path.GetFullPath(ServiceName); 46 Process.Start("sc", "create " + ServiceName + " binpath= "" + path + Extension + " s" + "" displayName= " + DisplayServiceName + " start= auto"); 47 Console.WriteLine("安装成功"); 48 break; 49 case 2: 50 if (IsServiceInstalled == false) 51 { 52 Console.WriteLine("服务未安装......"); 53 break; 54 } 55 using (ServiceController control = new ServiceController(ServiceName)) 56 { 57 if (control.Status == ServiceControllerStatus.Stopped) 58 { 59 Console.WriteLine("服务启动......"); 60 try 61 { 62 control.Start(); 63 Console.WriteLine("服务已经启动......"); 64 } 65 catch (Exception) 66 { 67 Console.WriteLine("无法启动服务!"); 68 } 69 } 70 } 71 break; 72 case 3: 73 if (IsServiceInstalled == false) 74 { 75 Console.WriteLine("服务未安装......"); 76 break; 77 } 78 using (ServiceController control = new ServiceController(ServiceName)) 79 { 80 if (control.Status == ServiceControllerStatus.Running) 81 { 82 Console.WriteLine("服务停止......"); 83 try 84 { 85 control.Stop(); 86 Console.WriteLine("服务已经停止......"); 87 } 88 catch (Exception) 89 { 90 Console.WriteLine("无法停止服务!"); 91 } 92 } 93 else if ((control.Status == ServiceControllerStatus.Stopped) 94 || (control.Status == ServiceControllerStatus.StopPending)) 95 { 96 Console.WriteLine("当前服务未启动......"); 97 } 98 } 99 break; 100 case 4: 101 if (IsServiceInstalled == false) 102 { 103 Console.WriteLine("服务未安装......"); 104 break; 105 } 106 Process.Start("sc", "delete " + ServiceName); 107 Console.WriteLine("卸载成功"); 108 break; 109 case 5: isExit = true; break; 110 default: 111 Console.WriteLine("输入错误!"); 112 isExit = true; 113 break; 114 } 115 } 116 } 117 } 118 /// <summary> 119 /// 判断服务是否已经安装 120 /// </summary> 121 /// <param name="serviceName"></param> 122 /// <returns></returns> 123 private static bool ISWindowsServiceInstalled(string serviceName) 124 { 125 try 126 { 127 return ServiceController.GetServices().Any(c => c.ServiceName == serviceName); 128 } 129 catch 130 { return false; } 131 } 132 } 133 }
第五步 配置log4net文件
新建配置文件
加入代码
1 <log4net> 2 <logger name="LogHelper"> 3 <level value="ALL" /> 4 <appender-ref ref="Appender" /> 5 </logger> 6 <logger name="LogHelperNormal"> 7 <level value="ALL" /> 8 <appender-ref ref="NormalAppender" /> 9 </logger> 10 <appender name="Appender" type="log4net.Appender.RollingFileAppender"> 11 <!--日志文件名开头--> 12 <param name="File" value="Log\" /> 13 <!--是否追加到文件,默认为true,通常无需设置--> 14 <param name="AppendToFile" value="true" /> 15 <param name="MaxSizeRollBackups" value="100" /> 16 <param name="MaxFileSize" value="10240" /> 17 <param name="StaticLogFileName" value="false" /> 18 <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置--> 19 <param name="DatePattern" value="yyyyMMdd"_Exception.htm"" /> 20 <param name="RollingStyle" value="Date" /> 21 <layout type="log4net.Layout.PatternLayout"> 22 <param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> 23 </layout> 24 </appender> 25 <appender name="NormalAppender" type="log4net.Appender.RollingFileAppender"> 26 <param name="File" value="Log\" /> 27 <param name="AppendToFile" value="true" /> 28 <param name="MaxFileSize" value="10240" /> 29 <param name="MaxSizeRollBackups" value="100" /> 30 <param name="StaticLogFileName" value="false" /> 31 <param name="DatePattern" value="yyyyMMdd"_Normal.htm"" /> 32 <param name="RollingStyle" value="Date" /> 33 <layout type="log4net.Layout.PatternLayout"> 34 <param name="ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> 35 </layout> 36 </appender> 37 </log4net>
完成.
最终文件形式如图
其中示例服务的loghelp是使用log4net自己封装了一个类库 使用时候需要把log4net.config文件选择复制到输出目录 不然不会打印日志
示例代码点击下方下载:(含loghelper)
网盘地址 链接: http://pan.baidu.com/s/1boWeHiv 密码: kjp5