一、创建Windows服务

 使用VS创建一个新的windows服务应用程序

C#  Windows服务创建安装卸载

 

C#  Windows服务创建安装卸载

 创建完成之后

C#  Windows服务创建安装卸载

 

 

 

 

二、相关配置

修改Service1名称为StartService(可以不改,自行选择

C#  Windows服务创建安装卸载

C#  Windows服务创建安装卸载

 

添加安装程序并修改配置

C#  Windows服务创建安装卸载

 

 

 

 安装完成之后,源码中会出现一个ProjectInstaller程序集,双击进入页面修改相关属性

 

 C#  Windows服务创建安装卸载              C#  Windows服务创建安装卸载

 

 

 

 C#  Windows服务创建安装卸载   C#  Windows服务创建安装卸载

 

 

 

 添加文件夹和实体类

C#  Windows服务创建安装卸载

 

 

 


 LogHelper.cs 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Reflection;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace WindowsService.Common
10 {
11     public class LogHelper
12     {
13         /// <summary>
14         /// 获取程序异常信息
15         /// </summary>
16         /// <param name="methodBase"></param>
17         /// <param name="exception"></param>
18         /// <param name="message"></param>
19         /// <returns></returns>
20         public static string GetExceptionMessage(MethodBase methodBase, Exception exception, string message)
21         {
22             string fullName = methodBase.ReflectedType.FullName;
23             string name = methodBase.Name;
24             string str = $"\r\n异常综合信息(类:{fullName};函数名称:{name};):\r\n";
25             str += "-----------\r\n";
26             str += ".Net异常信息:\r\n";
27             if (exception == null)
28             {
29                 str += "   无异常对象,也无堆栈信息(exception == null)\r\n";
30             }
31             else
32             {
33                 str += $"   {exception.Message}\r\n";
34                 str += $".Net堆栈信息:\r\n{exception.StackTrace}\r\n";
35             }
36             str += "-----------\r\n";
37             if (message != null && message.Trim().Length > 0)
38             {
39                 str += "===========\r\n";
40                 str += $"自定义信息:\r\n   {message}\r\n";
41                 str += "===========\r\n";
42             }
43             return str;
44         }
45 
46         /// <summary>
47         /// 写出日志信息 目录地址:string logPath = AppDomain.CurrentDomain.BaseDirectory + "00_Log\\";
48         /// </summary>
49         /// <param name="folderName"></param>
50         /// <param name="message"></param>
51         public static void Write(string folderName, string message)
52         {
53             string text = AppDomain.CurrentDomain.BaseDirectory + "00_Log\\";
54             if (folderName != null && folderName.Trim().Length > 0)
55             {
56                 text += folderName;
57             }
58             WritingLogs(text, message);
59         }
60 
61         /// <summary>
62         ///  写出异常日志(.txt)
63         /// </summary>
64         /// <param name="strPath"></param>
65         /// <param name="strContent"></param>
66         public static void WritingLogs(string strPath, string strContent)
67         {
68             FileStream fileStream = null;
69             StreamWriter streamWriter = null;
70             try
71             {
72                 if (!Directory.Exists(strPath))
73                 {
74                     Directory.CreateDirectory(strPath);
75                 }
76                 strPath = string.Format("{0}\\{1}{2}", strPath, DateTime.Now.ToString("yyyy-MM-dd"), ".txt");
77                 fileStream = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write);
78                 streamWriter = new StreamWriter(fileStream);
79                 streamWriter.BaseStream.Seek(0L, SeekOrigin.End);
80                 streamWriter.WriteLine(strContent + "\r\n\r\n--------------------------------------------------------" + DateTime.Now.ToString() + "--------------------------------------------------------\r\n");
81                 streamWriter.Flush();
82                 streamWriter.Close();
83                 fileStream.Close();
84             }
85             finally
86             {
87                 streamWriter.Close();
88                 fileStream.Close();
89             }
90         }
91 
92 
93     }
94 }
LogHelper

相关文章: