-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
99 lines (93 loc) · 4.16 KB
/
Program.cs
File metadata and controls
99 lines (93 loc) · 4.16 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using System;
using System.IO;
using System.Threading;
using System.Security.Principal;
using System.IO;
using System.Security.AccessControl;
namespace ProcessMonitorService
{
class Program
{
private static bool IsAdministrator()
{
using (var identity = WindowsIdentity.GetCurrent())
{
var principal = new WindowsPrincipal(identity);
// Prüft, ob der Benutzer in der Rolle "Administrator" ist.
// Der "System"-Benutzer hat ebenfalls administrative Rechte und wird hier korrekt erfasst.
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
static async Task Main(string[] args)
{
// Serilog Bootstrap-Logger für den Startvorgang
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"ProcessMonitorService",
"logs",
$"startup_error_{DateTime.Now:yyyyMMdd_HHmmss}.log"
),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
)
.CreateBootstrapLogger();
try
{
Log.Information("=== Process Monitor Service starting ===");
if (!IsAdministrator())
{
Log.Fatal("Application must be run with administrative privileges (or as System user). Exiting.");
// Optional: Eine Fehlermeldung auf die Konsole schreiben, falls es kein Dienst ist
if (Environment.UserInteractive)
{
Console.WriteLine("Error: This application requires administrative privileges to run. Please run as administrator.");
}
return; // Beendet die Anwendung, wenn keine Admin-Rechte vorliegen
}
await Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureAppConfiguration((context, config) =>
{
config.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.Enrich.WithProperty("MachineName", Environment.MachineName)
.Enrich.WithProperty("ThreadId", Thread.CurrentThread.ManagedThreadId)
)
.ConfigureServices((hostContext, services) =>
{
// Configuration
services.Configure<ProcessMonitorOptions>(
hostContext.Configuration.GetSection("ProcessMonitor"));
// Services
services.AddSingleton<IProcessOwnerService, ProcessOwnerService>();
services.AddHostedService<ProcessMonitorWorker>();
// Health checks (optional)
services.AddHealthChecks();
})
.Build()
.RunAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "Critical error: Host could not be started");
Environment.ExitCode = 1;
}
finally
{
await Log.CloseAndFlushAsync();
}
}
}
}