823e467078
- Introduced a batch script to simplify the startup process for the Resource Monitor Service. - Included checks for .NET 9.0 Runtime installation. - Added build and run commands for the service with appropriate error handling. - Provided user instructions and API documentation links in the script output.
72 lines
2.6 KiB
C#
72 lines
2.6 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using ResourceMonitorService.Configuration;
|
|
using ResourceMonitorService.Services;
|
|
using Serilog;
|
|
using System.Diagnostics;
|
|
|
|
namespace ResourceMonitorService
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
// Configure Serilog early
|
|
Log.Logger = new LoggerConfiguration()
|
|
.WriteTo.Console()
|
|
.WriteTo.File("logs/resourcemonitor-.txt", rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
|
|
try
|
|
{
|
|
Log.Information("Starting Resource Monitor Service");
|
|
CreateHostBuilder(args).Build().Run();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Application start-up failed");
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
}
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args)
|
|
{
|
|
var builder = Host.CreateDefaultBuilder(args)
|
|
.UseSerilog()
|
|
.ConfigureServices((hostContext, services) =>
|
|
{
|
|
// Bind configuration sections
|
|
services.Configure<MonitoringSettings>(
|
|
hostContext.Configuration.GetSection("MonitoringSettings"));
|
|
services.Configure<ApiSettings>(
|
|
hostContext.Configuration.GetSection("ApiSettings"));
|
|
services.Configure<LoggingSettings>(
|
|
hostContext.Configuration.GetSection("LoggingSettings"));
|
|
|
|
// Register services
|
|
services.AddSingleton<ISystemInfoService, SystemInfoService>();
|
|
services.AddSingleton<IResourceMonitorService, Services.ResourceMonitorService>();
|
|
services.AddSingleton<IGameDetectionService, GameDetectionService>();
|
|
services.AddSingleton<IAlertService, AlertService>();
|
|
|
|
// Register the main worker service
|
|
services.AddHostedService<Worker>();
|
|
});
|
|
|
|
// Configure as Windows Service if requested
|
|
if (args.Contains("--windows-service") || Environment.GetEnvironmentVariable("RUN_AS_SERVICE") == "true")
|
|
{
|
|
builder.UseWindowsService(options =>
|
|
{
|
|
options.ServiceName = "ResourceMonitorService";
|
|
});
|
|
}
|
|
|
|
return builder;
|
|
}
|
|
}
|
|
} |