From 1027f5f81e22538e64da148da633687571a120b8 Mon Sep 17 00:00:00 2001 From: noberumotto Date: Sun, 21 Nov 2021 00:04:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0log=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Servicers/Instances/Logger.cs | 61 ++++++++++++++++++++++++++++ Core/Servicers/Interfaces/ILogger.cs | 15 +++++++ 2 files changed, 76 insertions(+) create mode 100644 Core/Servicers/Instances/Logger.cs create mode 100644 Core/Servicers/Interfaces/ILogger.cs diff --git a/Core/Servicers/Instances/Logger.cs b/Core/Servicers/Instances/Logger.cs new file mode 100644 index 0000000..2f33a20 --- /dev/null +++ b/Core/Servicers/Instances/Logger.cs @@ -0,0 +1,61 @@ +using Core.Servicers.Interfaces; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Core.Servicers.Instances +{ + public class Logger : ILogger + { + private static readonly object writeLock = new object(); + private string loggerName; + private enum LogLevel + { + Info, + Warn, + Error, + } + public Logger() + { + loggerName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, + "Log", DateTime.Now.ToString("yyyy-MM-dd") + ".log"); + } + public void Info(string message) + { + Save(Fromat(LogLevel.Info, message)); + } + + public void Warn(string message) + { + Save(Fromat(LogLevel.Warn, message)); + } + public void Error(string message) + { + Save(Fromat(LogLevel.Error, message)); + } + + private string Fromat(LogLevel logLevel, string message) + { + string logText = $"[{logLevel.ToString()}] [{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] \r\n{message}\r\n------------------------\r\n\r\n"; + Debug.WriteLine(logText); + return logText; + } + + private void Save(string message) + { + lock (writeLock) + { + string dir = Path.GetDirectoryName(loggerName); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + File.AppendAllText(loggerName, message); + } + } + } +} diff --git a/Core/Servicers/Interfaces/ILogger.cs b/Core/Servicers/Interfaces/ILogger.cs new file mode 100644 index 0000000..5008293 --- /dev/null +++ b/Core/Servicers/Interfaces/ILogger.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Core.Servicers.Interfaces +{ + public interface ILogger + { + void Info(string message); + void Warn(string message); + void Error(string message); + } +} -- GitLab