-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathService1.cs
66 lines (57 loc) · 1.85 KB
/
Service1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace WindowsService.NET
{
public partial class Service1 : ServiceBase
{
Timer Timer = new Timer();
int Interval = 10000; // 10000 ms = 10 second
public Service1()
{
InitializeComponent();
this.ServiceName = "WindowsService.NET";
}
protected override void OnStart(string[] args)
{
WriteLog("Service has been started");
Timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
Timer.Interval = Interval;
Timer.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
WriteLog(String.Format("{0} ms elapsed.", Interval));
}
protected override void OnStop()
{
Timer.Stop();
WriteLog("Service has been stopped.");
}
public void WriteLog(string logMessage, bool addTimeStamp = true)
{
var path = AppDomain.CurrentDomain.BaseDirectory;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var filePath = String.Format("{0}\\{1}_{2}.txt",
path,
ServiceName,
DateTime.Now.ToString("yyyyMMdd", CultureInfo.CurrentCulture)
);
if (addTimeStamp)
logMessage = String.Format("[{0}] - {1}\r\n",
DateTime.Now.ToString("HH:mm:ss", CultureInfo.CurrentCulture),
logMessage);
File.AppendAllText(filePath, logMessage);
}
}
}