55using System . Text ;
66using System . Runtime . CompilerServices ;
77using EnergyStarZ . Config ;
8+ using EnergyStarZ . Utilities ;
89using Microsoft . Extensions . Configuration ;
910
1011namespace EnergyStarZ
@@ -172,7 +173,7 @@ public static void LoadConfiguration()
172173 }
173174 catch ( Exception ex )
174175 {
175- Console . WriteLine ( $ "[ERROR] Failed to load configuration: { ex . Message } ") ;
176+ AppLogger . Error ( $ "Failed to load configuration: { ex . Message } ") ;
176177 }
177178 }
178179
@@ -208,7 +209,7 @@ private void InitializeUnmanagedResources()
208209 }
209210 catch ( Exception ex )
210211 {
211- Console . WriteLine ( $ "[ERROR] Failed to initialize unmanaged resources: { ex . Message } ") ;
212+ AppLogger . Error ( $ "Failed to initialize unmanaged resources: { ex . Message } ") ;
212213 ReleaseUnmanagedResources ( ) ;
213214 throw ;
214215 }
@@ -224,7 +225,7 @@ private void StartTimers()
224225
225226 private static async Task RefreshProcessCacheLoop ( CancellationToken cancellationToken )
226227 {
227- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] Process cache refresh loop started.") ;
228+ AppLogger . Info ( " Process cache refresh loop started.") ;
228229
229230 int consecutiveFailures = 0 ;
230231 const int maxFailuresBeforeBackoff = 3 ;
@@ -243,12 +244,12 @@ private static async Task RefreshProcessCacheLoop(CancellationToken cancellation
243244 catch ( Exception ex )
244245 {
245246 consecutiveFailures ++ ;
246- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [ERROR] RefreshProcessCache (failure { consecutiveFailures } ): { ex . Message } ") ;
247+ AppLogger . Error ( $ "RefreshProcessCache (failure { consecutiveFailures } ): { ex . Message } ") ;
247248
248249 // 连续失败后退避
249250 if ( consecutiveFailures >= maxFailuresBeforeBackoff )
250251 {
251- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [WARN] Too many failures, backing off for { backoffSeconds } s") ;
252+ AppLogger . Warn ( $ "Too many failures, backing off for { backoffSeconds } s") ;
252253 await Task . Delay ( TimeSpan . FromSeconds ( backoffSeconds ) , cancellationToken ) ;
253254 consecutiveFailures = 0 ;
254255 continue ;
@@ -265,12 +266,12 @@ private static async Task RefreshProcessCacheLoop(CancellationToken cancellation
265266 }
266267 }
267268
268- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] Process cache refresh loop stopped.") ;
269+ AppLogger . Info ( " Process cache refresh loop stopped.") ;
269270 }
270271
271272 private static async Task PowerStatusCheckLoop ( CancellationToken cancellationToken )
272273 {
273- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] Power status check loop started.") ;
274+ AppLogger . Info ( " Power status check loop started.") ;
274275
275276 while ( ! cancellationToken . IsCancellationRequested )
276277 {
@@ -280,7 +281,7 @@ private static async Task PowerStatusCheckLoop(CancellationToken cancellationTok
280281 }
281282 catch ( Exception ex )
282283 {
283- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [ERROR] CheckPowerStatus: { ex . Message } ") ;
284+ AppLogger . Error ( $ "CheckPowerStatus: { ex . Message } ") ;
284285 }
285286
286287 try
@@ -293,7 +294,7 @@ private static async Task PowerStatusCheckLoop(CancellationToken cancellationTok
293294 }
294295 }
295296
296- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] Power status check loop stopped.") ;
297+ AppLogger . Info ( " Power status check loop stopped.") ;
297298 }
298299
299300 private static void CheckPowerStatus ( )
@@ -319,13 +320,13 @@ private static void CheckPowerStatus()
319320 {
320321 CurrentMode = PowerMode . Auto ;
321322 HookManager . SubscribeToWindowEvents ( ) ;
322- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [POWER STATUS] Switched to Battery Mode - Automatic energy saving enabled") ;
323+ AppLogger . Info ( " Switched to Battery Mode - Automatic energy saving enabled") ;
323324 }
324325 else
325326 {
326327 CurrentMode = PowerMode . Paused ;
327328 HookManager . UnsubscribeWindowEvents ( ) ;
328- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [POWER STATUS] Switched to AC Mode - Energy saving paused") ;
329+ AppLogger . Info ( " Switched to AC Mode - Energy saving paused") ;
329330 }
330331
331332 _lastSwitchTime = DateTime . UtcNow ;
@@ -369,7 +370,7 @@ private void Dispose(bool disposing)
369370 }
370371 catch ( Exception ex )
371372 {
372- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [WARNING] Error waiting for background tasks: { ex . Message } ") ;
373+ AppLogger . Warn ( $ "Error waiting for background tasks: { ex . Message } ") ;
373374 }
374375 finally
375376 {
@@ -509,7 +510,7 @@ private static void AutoAdjustLRUSize(DateTime now)
509510
510511 if ( targetSize != _lruCacheSize )
511512 {
512- Console . WriteLine ( $ "[ { now : O } ] [LRU] Auto-adjusting LRU size from { _lruCacheSize } to { targetSize } (switches/min: { switchesPerMinute : F1} , active apps: { activeAppCount } )") ;
513+ AppLogger . Info ( $ "Auto-adjusting LRU size from { _lruCacheSize } to { targetSize } (switches/min: { switchesPerMinute : F1} , active apps: { activeAppCount } )") ;
513514 _lruCacheSize = targetSize ;
514515
515516 // 如果缩小,淘汰最旧的条目
@@ -570,7 +571,7 @@ private static void ToggleEfficiencyMode(IntPtr hProcess, bool enable)
570571 }
571572 catch ( Exception ex )
572573 {
573- Console . WriteLine ( $ "Error applying efficiency mode to process: { ex . Message } ") ;
574+ AppLogger . Error ( $ "Error applying efficiency mode to process: { ex . Message } ") ;
574575 }
575576 }
576577
@@ -583,7 +584,7 @@ private static void RefreshProcessCache()
583584 }
584585 catch ( Exception ex )
585586 {
586- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [ERROR] Failed to get processes: { ex . Message } ") ;
587+ AppLogger . Error ( $ "Failed to get processes: { ex . Message } ") ;
587588 return ;
588589 }
589590
@@ -684,7 +685,7 @@ private static void ApplyEfficiencyMode(IEnumerable<ProcessInfo> processes, uint
684685 }
685686 catch ( Exception ex )
686687 {
687- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [ERROR] ApplyEfficiencyMode failed for { procInfo . Name } (PID: { procInfo . Id } ): { ex . Message } ") ;
688+ AppLogger . Error ( $ "ApplyEfficiencyMode failed for { procInfo . Name } (PID: { procInfo . Id } ): { ex . Message } ") ;
688689 }
689690 finally
690691 {
@@ -730,7 +731,7 @@ await Task.Run(() =>
730731 }
731732 catch ( Exception ex )
732733 {
733- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [ERROR] RestoreAllProcessesToNormal failed for { procInfo . Name } (PID: { procInfo . Id } ): { ex . Message } ") ;
734+ AppLogger . Error ( $ "RestoreAllProcessesToNormal failed for { procInfo . Name } (PID: { procInfo . Id } ): { ex . Message } ") ;
734735 }
735736 finally
736737 {
@@ -845,12 +846,12 @@ public static void HandleForegroundEvent(IntPtr hwnd)
845846 AddOrUpdateApp ( actualProcId , appName ) ;
846847
847848 // Boost the current foreground app
848- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [FOREGROUND] Boost { appName } ") ;
849+ AppLogger . Info ( $ "[FOREGROUND] Boost { appName } ") ;
849850 ToggleEfficiencyMode ( actualProcHandle , false ) ;
850851 }
851852 else
852853 {
853- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [FOREGROUND] Skip protected process: { appName } ") ;
854+ AppLogger . Info ( $ "[FOREGROUND] Skip protected process: { appName } ") ;
854855 }
855856
856857 // 防抖:距离上次切换太近则跳过
@@ -879,13 +880,13 @@ public static void HandleForegroundEvent(IntPtr hwnd)
879880 var prevAppName = GetProcessNameFromHandle ( prevProcHandle ) ;
880881 if ( ! string . IsNullOrEmpty ( prevAppName ) && ! IsBypassProcess ( prevAppName . AsSpan ( ) ) )
881882 {
882- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [BACKGROUND] Throttle { prevAppName } ") ;
883+ AppLogger . Info ( $ "[BACKGROUND] Throttle { prevAppName } ") ;
883884 ToggleEfficiencyMode ( prevProcHandle , true ) ;
884885 }
885886 }
886887 catch ( Exception ex )
887888 {
888- Console . WriteLine ( $ "[ { DateTime . UtcNow : O } ] [ERROR] Failed to throttle previous process: { ex . Message } ") ;
889+ AppLogger . Error ( $ "Failed to throttle previous process: { ex . Message } ") ;
889890 }
890891 finally
891892 {
0 commit comments