浏览代码

base file log

ganahrhr 3 年之前
父节点
当前提交
fd344bfbbd
共有 3 个文件被更改,包括 54 次插入3 次删除
  1. 9 0
      Program.cs
  2. 1 1
      Startup.cs
  3. 44 2
      Utils/AdvConsole.cs

+ 9 - 0
Program.cs

@@ -13,9 +13,18 @@ namespace HyperCube
     {
         public static void Main(string[] args)
         {
+            Utils.AdvConsole.Initialize();
+            System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
+
             CreateHostBuilder(args).Build().Run();
         }
 
+        private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
+        {
+            Exception ex = (Exception)e.ExceptionObject;
+            Utils.AdvConsole.WriteLine($"UNHANDLED EXCEPTION: {ex.GetType()}\r\n{ex.Message}\r\n{ex.StackTrace}");
+        }
+
         public static IHostBuilder CreateHostBuilder(string[] args) =>
             Host.CreateDefaultBuilder(args)
                 .ConfigureWebHostDefaults(webBuilder =>

+ 1 - 1
Startup.cs

@@ -35,7 +35,7 @@ namespace HyperCube
                 Console.WriteLine(e.Message + ", stack trace:" + e.StackTrace);
             }
 
-            Console.WriteLine($"Paths:\r\nApp:{AppDomain.CurrentDomain.BaseDirectory}\r\nWorking: {Environment.CurrentDirectory}");
+            Console.WriteLine($"Paths, app: [{AppDomain.CurrentDomain.BaseDirectory}], working: [{Environment.CurrentDirectory}]");
         }
 
         public IConfiguration Configuration { get; }

+ 44 - 2
Utils/AdvConsole.cs

@@ -1,12 +1,54 @@
 using System;
+using System.IO;
 
 namespace HyperCube.Utils
 {
     public static class AdvConsole
     {
-        public static void WriteLine(string message)
+        private const string LOGS_FOLDER = "Logs";
+        //private const string LOGS_ARCHIVE_FOLDER = "Archives";
+        private const string LOG_FILE_NAME = "current.log";
+
+        static StreamWriter _writer;
+        static TextWriter _oldOut = Console.Out;
+
+        static string _folderPath;
+        static string _fullPath;
+
+        public static void Initialize()
         {
-            Console.WriteLine($"{DateTime.Now:yyyy.MM.dd HH:mm:ss.fff}: {message}");            
+            _folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LOGS_FOLDER);
+            _fullPath = Path.Combine(_folderPath, LOG_FILE_NAME);
+            _oldOut = Console.Out;
+
+            if (File.Exists(_fullPath))
+                File.Move(_fullPath, Path.Combine(_folderPath, $"{DateTime.Now:yyyy.MM.dd_HH-mm-ss}.log"));
+        }
+
+        public static void WriteLine(string message)
+        {   
+            try
+            {
+                if (!Directory.Exists(_folderPath))
+                    Directory.CreateDirectory(_folderPath);
+
+                if (File.Exists(_fullPath))
+                    _writer = File.AppendText(_fullPath);
+                else
+                    _writer = File.CreateText(_fullPath);
+            }
+            catch (Exception e)
+            {
+                Console.WriteLine($"Cannot open [{_fullPath}] for writing");
+                Console.WriteLine(e.Message);
+                return;
+            }
+            string fullmessage = $"{DateTime.Now:yyyy.MM.dd HH:mm:ss.fff}: {message}";
+            Console.SetOut(_writer);
+            Console.WriteLine(fullmessage);
+            Console.SetOut(_oldOut);
+            Console.WriteLine(fullmessage);
+            _writer.Close();
         }       
     }
 }