123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System;
- using System.IO;
- using System.Text;
- namespace AIDiagnosisDemo.Infrastucture
- {
- public class Logger
- {
- private static readonly object Locker = new object();
- private static string LogDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
- static Logger()
- {
- }
- private static string FilePath
- {
- get { return Path.Combine(LogDir, DateTime.Now.ToString("yyyyMMdd") + ".txt"); }
- }
- private static void Write(string msg)
- {
- lock (Locker)
- {
- if (!File.Exists(FilePath))
- {
- if (!Directory.Exists(LogDir))
- Directory.CreateDirectory(LogDir);
- }
- using (var writer = new StreamWriter(FilePath, true, Encoding.UTF8))
- {
- writer.WriteLine(msg);
- }
- }
- }
- public static void Debug(string msg)
- {
- var now = DateTime.Now;
- msg = string.Format("[{0} {1}] : {2}", "Debug", $"{now:yyyyMMddHHmmss}_{now.Millisecond:d3}", msg);
- Write(msg);
- }
- public static void Info(string msg)
- {
- var now = DateTime.Now;
- msg = string.Format("[{0} {1}] : {2}", "Info", $"{now:yyyyMMddHHmmss}_{now.Millisecond:d3}", msg);
- Write(msg);
- }
- public static void Error(string msg)
- {
- var now = DateTime.Now;
- msg = string.Format("[{0} {1}] : {2}", "Error", $"{now:yyyyMMddHHmmss}_{now.Millisecond:d3}", msg);
- Write(msg);
- }
- public static void Error(string desc, Exception exception)
- {
- var now = DateTime.Now;
- var msg = string.Format("[{0} {1}] : {2} {3}", "Error", $"{now:yyyyMMddHHmmss}_{now.Millisecond:d3}", desc, exception);
- Write(msg);
- }
- }
- }
|