1. 新建项目 --- windows桌面 --- windows服务
2. 右击 ---- 添加安装程序
3. 右击 --- 属性
4. 右击service1查看代码
public partial class Service1 : ServiceBase { private int num = 1; public Service1() { InitializeComponent(); } /// <summary> /// 启动服务 /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { serviceLog("service Start."); // Set up a timer that triggers every minute. 设置定时器 Timer timer = new Timer(); timer.Interval = 60000; // 60 seconds 60秒执行一次 timer.Elapsed += new ElapsedEventHandler(this.OnTimer); timer.Start(); } /// <summary> /// 停止服务 /// </summary> protected override void OnStop() { serviceLog("service Stop."); } /// <summary> /// 继续服务 /// </summary> protected override void OnContinue() { serviceLog("service OnContinue."); } /// <summary> /// 定时器中定时执行的任务 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void OnTimer(object sender, ElapsedEventArgs args) { DataLog dataLog = new DataLog(); #region//执行的事件 try { ////记录数据日志 dataLog.WriteLogEx("第" + num + "次执行!"); num++; } catch (Exception ex) { dataLog.ErrorLog(ex.Message); } #endregion serviceLog("service Execute."); } /// <summary> /// 服务日志记录到指定路径:D:\serviceLog.txt /// </summary> /// <param name="message"></param> private static void serviceLog(string message) { using (FileStream stream = new FileStream("D:\\serviceLog.txt", FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now}:{message}"); } } }
5. 日志类
public class DataLog { #region 写日志 public void WriteLogEx(string dataInfo, string fileName = "") { string path = AppDomain.CurrentDomain.BaseDirectory; string logDir = "log"; logDir += "\\" + DateTime.Now.ToString("yyyy") + "\\" + DateTime.Now.ToString("MM"); path = Path.Combine(path, logDir); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } try { StringBuilder log = new StringBuilder(); log.Append(Environment.NewLine); log.Append("================BEGIN"); log.Append(DateTime.Now); log.Append("================"); log.Append(Environment.NewLine); log.Append(dataInfo); log.Append(Environment.NewLine); log.Append("================END "); log.Append(DateTime.Now); log.Append("================"); log.Append(Environment.NewLine); fileName = string.IsNullOrWhiteSpace(fileName) ? DateTime.Now.Day.ToString() + "日" : fileName; fileName = fileName + ".txt"; string file = Path.Combine(path, fileName); System.IO.File.AppendAllText(file, log.ToString(), System.Text.Encoding.Default); } catch { } } #endregion #region 错误日志 public void ErrorLog(string ExInfo, string fileName = "") { string path = AppDomain.CurrentDomain.BaseDirectory; string logDir = "ErrLog"; logDir += "\\" + DateTime.Now.ToString("yyyy") + "\\" + DateTime.Now.ToString("MM"); path = Path.Combine(path, logDir); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } try { StringBuilder log = new StringBuilder(); log.Append(Environment.NewLine); log.Append("================BEGIN"); log.Append(DateTime.Now); log.Append("================"); log.Append(Environment.NewLine); log.Append(ExInfo); log.Append(Environment.NewLine); log.Append("================END "); log.Append(DateTime.Now); log.Append("================"); log.Append(Environment.NewLine); fileName = string.IsNullOrWhiteSpace(fileName) ? DateTime.Now.Day.ToString() + "日" : fileName; fileName = fileName + ".txt"; string file = Path.Combine(path, fileName); System.IO.File.AppendAllText(file, log.ToString(), System.Text.Encoding.Default); } catch { } } #endregion }
6. 重新生成解决方案
7. 打开服务安装程序安装启动服务
选择生成的exe文件(bin/debug文件夹下.exe文件)
先安装 >> 再启动
8. 我们来看看执行结果
9. 先停止服务,再卸载服务
服务安装程序分享(
链接:https://pan.baidu.com/s/1sAiHj-WJ2bq00sn-s0Z6CA
提取码:gal0
)