1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.IO;
- using System.Text;
- using System.Threading;
- namespace AICustomizeSDKDemo.Infrastucture
- {
- public class Logger
- {
- private static string _filePath;
- private static readonly object Locker = new object();
- private static string _logDir;
- public static string LogDir
- {
- get => _logDir;
- set
- {
- if (_logDir != value)
- {
- _logDir = value;
- if (!Directory.Exists(LogDir))
- {
- Directory.CreateDirectory(LogDir);
- }
- var now = DateTime.Now;
- var logTag = $"{now:yyyyMMddHHmmss}_{now.Millisecond:d3}";
- _filePath = Path.Combine(LogDir, $"Log_{logTag}.log");
- }
- }
- }
- static Logger()
- {
- LogDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
- }
- private static void Write(string msg)
- {
- lock (Locker)
- {
- using (var writer = new StreamWriter(_filePath, true, Encoding.UTF8))
- {
- writer.WriteLine(msg);
- }
- }
- }
- public static void Debug(string msg)
- {
- var now = DateTime.Now;
- msg = $"[{now:yyyyMMddHHmmss},{now.Millisecond:d3}]-D: [Thread:{Thread.CurrentThread.ManagedThreadId}]{msg}";
- Write(msg);
- }
- public static void Info(string msg)
- {
- var now = DateTime.Now;
- msg = $"[{now:yyyyMMddHHmmss},{now.Millisecond:d3}]-I: [Thread:{Thread.CurrentThread.ManagedThreadId}]{msg}";
- Write(msg);
- }
- public static void Error(string msg)
- {
- var now = DateTime.Now;
- msg = $"[{now:yyyyMMddHHmmss},{now.Millisecond:d3}]-E: [Thread:{Thread.CurrentThread.ManagedThreadId}]{msg}";
- Write(msg);
- }
- public static void Warning(string msg)
- {
- var now = DateTime.Now;
- msg = $"[{now:yyyyMMddHHmmss},{now.Millisecond:d3}]-W: [Thread:{Thread.CurrentThread.ManagedThreadId}]{msg}";
- Write(msg);
- }
- }
- }
|