C# 代码实现 Windows服务:计时器=>简单实现
作者:互联网
输入项目名称,选择框架直接生成项目
点击添加
选择属性后更换
将USER 改为 LOCALSYSTEM
可以自己更改serviceInstaller1 和servicename
SeriviceName:就是你等下装到系统里面的服务名称
在更改的页面右击查看代码或者F7
继承serviceBase,重写onstart(),onstop()方法
这两个方法记录服务启动和停止,在这两个方法里面增加写日志的方法,当服务启动停止可以直接查看日志
两种写入的方法,都很扣脚
using System; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.ServiceProcess; using System.Timers; namespace WindowsServiceCode { [RunInstaller(true)] public partial class ProjectInstaller : ServiceBase { //记录到event log中,地址是 C:\Windows\System32\winevt\Logs (双击查看即可,文件名为MyNewLog) private static EventLog eventLog1; //记录执行的次数 private int eventId = 1; public ProjectInstaller() { InitializeComponent(); eventLog1 = new System.Diagnostics.EventLog(); if (!System.Diagnostics.EventLog.SourceExists("MySource")) { System.Diagnostics.EventLog.CreateEventSource( "MySource", "MyNewLog"); } eventLog1.Source = "MySource"; eventLog1.Log = "MyNewLog"; } private void TimedEvent(object sender, ElapsedEventArgs e) { //定时器定时执行的方法 using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "RunTime.+执行方法" + "\n"); } eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId++); log("RunTime.+执行方法"); } /// <summary> /// 开始服务 /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { FileStream fs = new FileStream(@"C:\log.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine(DateTime.Now.ToString() + "OnStartMethod1-1" + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "OnStartMethod1-2" + "\n"); } #region 又来记录日志的方法 eventLog1.WriteEntry("OnStartMethod1-3"); log("OnStartMethod1-4"); #endregion 又来记录日志的方法 #region 定时器定义 Timer aTimer = new Timer();//System.Timers,不是form的 aTimer.Elapsed += new ElapsedEventHandler(TimedEvent); aTimer.Interval = 2 * 1000; //配置文件中配置的秒数 2 seconds 执行一次 aTimer.Enabled = true; aTimer.Start(); #endregion 定时器定义 } /// <summary> /// 停止服务 /// </summary> protected override void OnStop() { FileStream fs = new FileStream(@"C:\log.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine(DateTime.Now.ToString() + "OnStopMethod2-1" + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "OnStopMethod2-2" + "\n"); } eventLog1.WriteEntry("OnStopMethod2-3"); log("OnStopMethod2-4"); } /// <summary> /// 继续服务 /// </summary> protected override void OnContinue() { eventLog1.WriteEntry("In OnContinue.-继续服务"); log("In OnContinue.-继续服务"); } /// <summary> /// 记录到指定路径:D:\log.txt /// </summary> /// <param name="message"></param> private static void log(string message) { using (FileStream stream = new FileStream("D:\\log.txt", FileMode.Append)) using (StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine($"{DateTime.Now}:{message}"); } } } }View Code
复制C:\Windows\Microsoft.NET\Framework\v4.0.30319 下的InstallUtil.exe到生成的文件夹下
CMD先进入刚刚所在的文件夹
CMD管理员身份运行刚刚VS 生成的目录
剩下的今天来不及了,以后再记录吧
转载参考连接:http://www.vnfan.com/robin/d/9c9cdea813e068d9.html
标签:StreamWriter,log,streamWriter,C#,System,Windows,计时器,using,new 来源: https://www.cnblogs.com/fei-tang/p/13960627.html