WindowsProgramMonitorService

WindowsProgramMonitorService


Topshelf

Nuget

1
Install-Package Topshelf

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var rc = HostFactory.Run(x => {

x.Service<ProgramService>(s =>
{
s.ConstructUsing(name => new ProgramService());
s.WhenStarted(p => p.Start());
s.WhenStopped(p => p.Stop());
});

x.RunAsLocalSystem();

x.SetDescription("Windows Program Monitor Service Host");
x.SetDisplayName("Windows Program Monitor Service");
x.SetServiceName("Windows Program Monitor Service");
});

ProgramService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
readonly Timer _timer;
public ProgramService()
{
_timer = new Timer(1000) { AutoReset = true };
_timer.Elapsed +=
(sender, eventArgs) =>
Console.WriteLine($"It is {DateTime.Now} and all is well");
}

public void Start()
{
_timer.Start();
}

public void Stop()
{
_timer.Stop();
}

Program

GETProcess

GetMainProcess()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public string GetMainProcess()
{

var processlist = Process.GetProcesses();
var result = "";

foreach (Process process in processlist)
{
if (!String.IsNullOrEmpty(process.MainWindowTitle))
{
result += $"Process: {process.ProcessName}, Window title: {process.MainWindowTitle},Start Time : {process.StartTime} ,Running Time: {DateTime.Now - process.StartTime}\n";
}
}
return result;
}

log4net

Nuget

Nuget
1
Install-Package log4net

###

REF

Topself Quick Start

log4net configuration

评论