-
Notifications
You must be signed in to change notification settings - Fork 6
/
Service.cs
52 lines (51 loc) · 1.41 KB
/
Service.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
using System;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace OmahaDemoService
{
public partial class Service : ServiceBase
{
Timer timer = new Timer();
string version = "0.0.0.2";
public Service()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Log("started");
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 5000;
timer.Enabled = true;
}
protected override void OnStop()
{
Log("stopped");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
Log("still running");
}
public void Log(string Message)
{
string fullMessage = DateTime.Now + " v" + version + " " + Message;
string filepath = "C:\\OmahaDemoService.log";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(fullMessage);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(fullMessage);
}
}
}
}
}