Files
XRServer/XRServer/Logger.cs
2025-06-02 11:41:39 +09:00

153 lines
5.7 KiB
C#

using log4net;
using log4net.Config;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace utils
{
public class Logger
{
//private static readonly log4net.ILog logger = log4net.LogManager.GetLogger("Logger");
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static Logger()
{
try
{
//log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4netSqlite.config"));
string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log4net.config");
FileInfo configFile = new FileInfo(configFilePath);
if (configFile.Exists)
{
XmlConfigurator.ConfigureAndWatch(configFile);
}
else
{
throw new FileNotFoundException("log4net configuration file not found", configFilePath);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error configuring log4net: {ex.Message}");
throw;
}
}
public static void Info(string msg)
{
StackFrame frame = (new StackTrace(true)).GetFrame(1);
var method = frame.GetMethod();
var type = method.DeclaringType;
var name = method.Name;
int lineNumber = frame.GetFileLineNumber();
if (logger.IsInfoEnabled) Log(logType.Info, type.Name + "." + name + " [line " + lineNumber + "] " + msg);
}
public static void Debug(string msg)
{
StackFrame frame = (new StackTrace(true)).GetFrame(1);
var method = frame.GetMethod();
var type = method.DeclaringType;
var name = method.Name;
int lineNumber = frame.GetFileLineNumber();
if (logger.IsDebugEnabled) Log(logType.Debug, $"{(type.FullName == null ? type.Name : type.FullName)}.{name} [line {lineNumber}] {msg}");
}
public static void Warning(string msg, Exception ex)
{
StackFrame frame = (new StackTrace(true)).GetFrame(1);
var method = frame.GetMethod();
var type = method.DeclaringType;
var name = method.Name;
int lineNumber = frame.GetFileLineNumber();
if (logger.IsWarnEnabled) Log(logType.Warning, type.Name + "." + name + " [line " + lineNumber + "] " + msg, ex: ex);
}
public static void Error(string msg, Exception ex)
{
StackFrame frame = (new StackTrace(true)).GetFrame(1);
var method = frame.GetMethod();
var type = method.DeclaringType;
var name = method.Name;
int lineNumber = frame.GetFileLineNumber();
if (logger.IsErrorEnabled) Log(logType.Error, type.Name + "." + name + " [line " + lineNumber + "] " + msg, ex: ex);
}
public static void Fatal(string msg, Exception ex)
{
StackFrame frame = (new StackTrace(true)).GetFrame(1);
var method = frame.GetMethod();
var type = method.DeclaringType;
var name = method.Name;
int lineNumber = frame.GetFileLineNumber();
if (logger.IsFatalEnabled) Log(logType.Fatal, type.Name + "." + name + " [line " + lineNumber + "] " + msg, ex: ex);
}
// log4net 설정 파일 로드 App.xml.cs OnStartup
//XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
//
//각 클래스 마다
//static ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//
//private static ILog? _log;
//private static ILog log
//{
// get
// {
// if (_log == null)
// {
// _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// }
// return _log;
// }
//}
private enum logType : ushort
{
Debug = 0x10,
Info = 0x01,
Warning = 0x02,
Error = 0x04,
Fatal = 0x08
}
private static Dictionary<logType, Action<string, Exception>> logAction =
new Dictionary<logType, Action<string, Exception>>()
{
{ logType.Debug, (msg,info) => logger.Debug(msg)},
{ logType.Info, (msg,info) => logger.Info(msg)},
{ logType.Warning, (info,ex) => logger.Warn(info,ex)},
{ logType.Error, (info,ex) => logger.Error(info,ex)},
{ logType.Fatal, (info,ex) => logger.Fatal(info,ex)},
};
private static void Log(logType type, string msg = default(string), string info = default(string), Exception ex = default(Exception))
{
if ((logType.Error | logType.Warning | logType.Fatal).HasFlag(type))
//logAction[type](info, ex);
ThreadPool.QueueUserWorkItem(tmp => logAction[type](info, ex));
else
//logAction[type](msg, null);
ThreadPool.QueueUserWorkItem(tmp => logAction[type](msg, null));
}
//static public void Debug(string msg) => Log(logType.Debug, msg);
//static public void Info(string msg) => Log(logType.Info, msg);
//static public void Warning(string info, Exception ex) => Log(logType.Warning, info, ex: ex);
//static public void Error(string info, Exception ex) => Log(logType.Error, info, ex: ex);
//static public void Fatal(string info, Exception ex) => Log(logType.Fatal, info, ex: ex);
}
}