Initial commit

v1.0
This commit is contained in:
2023-03-14 17:30:09 +09:00
parent fa3fe72e21
commit 59dac350d8
25 changed files with 2072 additions and 0 deletions

View File

@@ -0,0 +1,174 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace HSUCO_Server_Monitoring
{
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파일 이름 세팅을 위한 함수 호출
}
/// <summary>
/// 로그 파일 리스너 세팅
/// </summary>
/// <param name="dateTimeString">로그파일 이름에 들어갈 DateTime String</param>
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);
}
}
/// <summary>
///
/// </summary>
/// <param name="logLevel">enum LogLevel</param>
/// <param name="message">로그 메세지</param>
public 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
}
}