using System; using System.Diagnostics; using System.IO; using System.Text; namespace CommonLibrary { public static class Log { //로그파일 이름 규칙 어셈블리명-날짜.log public static LogLevel logLevel { get; set; } private static string logDateTime; static Log() { SetLogFile(DateTime.Now.ToString("yyyy-MM-dd")); // Log파일 이름 세팅을 위한 함수 호출 logLevel = LogLevel.Info; } /// /// 로그 파일 리스너 세팅 /// /// 로그파일 이름에 들어갈 DateTime String private static void SetLogFile(string dateTimeString) { logDateTime = dateTimeString; if (!System.IO.Directory.Exists("logs")) { System.IO.Directory.CreateDirectory("logs"); } var logFileName = $"logs\\{logDateTime}.log"; Trace.Listeners.Clear(); Trace.Listeners.Add(new TextWriterTraceListener(new FileStream(logFileName, FileMode.Append, FileAccess.Write, FileShare.Read))); Trace.Write("====================================================================================================// "); Trace.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); Trace.WriteLine(" //===================================================================================================="); Trace.Flush(); } //FATAL > ERROR > WARN > INFO > DEBUG > TRACE public static void FATAL(string message) { if (LogLevel.FATAL >= logLevel) { // Call Location var stackTrace = new StackTrace(); var methodBase = stackTrace.GetFrame(1).GetMethod(); string location = methodBase.DeclaringType != null ? methodBase.DeclaringType.Name + "/" : "null/"; location += methodBase.Name; // Call Location End WriteLog(LogLevel.FATAL, location + "\t" + message); } } public static void ERROR(string message) { if (LogLevel.Error >= logLevel) { // Call Location var stackTrace = new StackTrace(); var methodBase = stackTrace.GetFrame(1).GetMethod(); string location = methodBase.DeclaringType != null ? methodBase.DeclaringType.Name + "/" : "null/"; location += methodBase.Name; // Call Location End WriteLog(LogLevel.Error, location + "\t" + message); } } public static void WARN(string message) { if (LogLevel.Warn >= logLevel) { // Call Location var stackTrace = new StackTrace(); var methodBase = stackTrace.GetFrame(1).GetMethod(); string location = methodBase.DeclaringType != null ? methodBase.DeclaringType.Name + "/" : "null/"; location += methodBase.Name; // Call Location End WriteLog(LogLevel.Warn, location + "\t" + message); } } public static void INFO(string message) { if (LogLevel.Info >= logLevel) { // Call Location var stackTrace = new StackTrace(); var methodBase = stackTrace.GetFrame(1).GetMethod(); string location = methodBase.DeclaringType != null ? methodBase.DeclaringType.Name + "/" : "null/"; location += methodBase.Name; // Call Location End WriteLog(LogLevel.Info, location + "\t" + message); } } public static void DEBUG(string message) { if (LogLevel.Debug >= logLevel) { // Call Location var stackTrace = new StackTrace(); var methodBase = stackTrace.GetFrame(1).GetMethod(); string location = methodBase.DeclaringType != null ? methodBase.DeclaringType.Name + "/" : "null/"; location += methodBase.Name; // Call Location End WriteLog(LogLevel.Debug, location + "\t" + message); } } public static void TRACE(string message) { if (LogLevel.Trace >= logLevel) { var stackTrace = new StackTrace(); var methodBase = stackTrace.GetFrame(1).GetMethod(); string location = methodBase.DeclaringType != null ? methodBase.DeclaringType.Name + "/" : "null/"; location += methodBase.Name; // Call Location End WriteLog(LogLevel.Debug, location + "\t" + message); WriteLog(LogLevel.Trace, message); } } /// /// /// /// enum LogLevel /// 로그 메세지 private static void WriteLog(LogLevel logLevel, string message) { string nowDateTime = DateTime.Now.ToString("yyyy-MM-dd"); if (nowDateTime != logDateTime) { SetLogFile(nowDateTime); } // Log String var stringBuilder = new StringBuilder(); stringBuilder.Append("["); stringBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); stringBuilder.Append("]"); stringBuilder.Append("\t"); stringBuilder.Append(logLevel); stringBuilder.Append("\t"); stringBuilder.Append(message.Trim()); Trace.WriteLine(stringBuilder.ToString()); Trace.Flush(); } } public enum LogLevel { Trace, Debug, Info, Warn, Error, FATAL } }