using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PCMonitor {
///
/// Main Agent Entry Point
/// Windows Service untuk monitoring PC
///
public class Program {
private static readonly ILog logger = new FileLogger();
private static AgentService agentService;
private static ServiceWrapper serviceWrapper;
[STAThread]
static void Main(string[] args) {
try {
AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
logger.Error($"Unhandled exception: {e.ExceptionObject}");
};
if (Environment.UserInteractive && args.Length > 0 && args[0].ToLower() == "debug") {
// Run in debug mode
RunDebugMode();
} else {
// Run as Windows Service
RunAsService();
}
} catch (Exception ex) {
logger.Error($"Fatal error: {ex.Message}");
if (Environment.UserInteractive) {
Console.WriteLine($"ERROR: {ex.Message}");
Console.ReadKey();
}
}
}
private static void RunDebugMode() {
Console.WriteLine("=== PC Monitor Agent (Debug Mode) ===");
Console.WriteLine($"Version: {GetVersion()}");
Console.WriteLine($"PC Name: {Environment.MachineName}");
Console.WriteLine();
agentService = new AgentService();
Console.WriteLine("Starting agent service...");
agentService.Start();
Console.WriteLine("Agent running. Press 'q' to quit...");
while (Console.ReadLine()?.ToLower() != "q") {
Console.WriteLine("Still running...");
}
Console.WriteLine("Stopping agent...");
agentService.Stop();
Console.WriteLine("Done.");
}
private static void RunAsService() {
agentService = new AgentService();
serviceWrapper = new ServiceWrapper(agentService);
System.ServiceProcess.ServiceBase.Run(serviceWrapper);
}
private static string GetVersion() {
var version = Assembly.GetExecutingAssembly().GetName().Version;
return $"{version.Major}.{version.Minor}.{version.Build}";
}
}
///
/// Logging Interface
///
public interface ILog {
void Info(string message);
void Warning(string message);
void Error(string message);
void Debug(string message);
}
///
/// File-based Logger
///
public class FileLogger : ILog {
private readonly string _logPath;
private readonly object _lockObj = new object();
public FileLogger() {
_logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"PCMonitor", "Logs"
);
Directory.CreateDirectory(_logPath);
}
private void WriteLog(string level, string message) {
lock (_lockObj) {
try {
string fileName = Path.Combine(_logPath, $"Agent_{DateTime.Now:yyyy-MM-dd}.log");
string logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{level}] {message}";
File.AppendAllText(fileName, logEntry + Environment.NewLine);
} catch {
// Silent fail if logging fails
}
}
}
public void Info(string message) => WriteLog("INFO", message);
public void Warning(string message) => WriteLog("WARN", message);
public void Error(string message) => WriteLog("ERROR", message);
public void Debug(string message) => WriteLog("DEBUG", message);
}
}