Files
2025-11-28 16:54:56 +09:00

123 lines
3.6 KiB
C#

using NLog;
using NLog.Config;
using NLog.Fluent;
namespace ServerCore;
// HANDOVER: NLog SDK Wrapper 클래스 이다.
// Log.sequence() 사용시 작성된 로그를 PlantUML의 시퀀스 다이어그램으로 볼 수 있다.
//===========================================================================================
// 로그 처리자
//
// author : kangms
//
//===========================================================================================
public static class Log
{
public static string NLogFileName { get; set; } = "./Config/nlog.config";
private static string m_process_type = string.Empty;
private static string m_ip_port = string.Empty;
private static string m_default_logger_name_pattern = string.Empty;
private static bool m_is_initialized = false;
private static bool m_is_reconfigure = true;
public static void initLog(string processType, string defaultLoggerNamePattern = ""
, EventHandler<LoggingConfigurationChangedEventArgs>? handler = null)
{
m_process_type = processType;
m_default_logger_name_pattern = defaultLoggerNamePattern;
if (null != handler)
{
registerConfigurationChangedHandler(handler);
}
m_is_initialized = true;
}
public static void setIPnPort(string ip, UInt16 port)
{
m_ip_port = $"{ip}:{port}";
}
public static NLog.Logger getLogger(string loggerName = "")
{
if (true == m_is_reconfigure)
{
reconfigureXML(NLogFileName);
}
if (0 == loggerName.Length)
{
loggerName = m_default_logger_name_pattern;
}
return LogManager.GetLogger(loggerName);
}
private static void registerConfigurationChangedHandler(EventHandler<LoggingConfigurationChangedEventArgs> handler)
{
LogManager.ConfigurationChanged += handler;
}
private static bool reconfigureXML(string xmlCofigFilePath)
{
if (true == m_is_reconfigure)
{
var to_reload_configure = new NLog.Config.XmlLoggingConfiguration(xmlCofigFilePath);
if (null == to_reload_configure)
{
return false;
}
LogManager.Configuration = to_reload_configure;
LogManager.ReconfigExistingLoggers();
m_is_reconfigure = false;
}
return true;
}
public static void shutdown()
{
CloudWatchLog.setClosed();
LogManager.Shutdown();
}
public static void sequence(string sender, string message)
{
NLog.Logger sequence_logger = LogManager.GetLogger("SequenceLogger");
LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Debug, "SequenceLogger", message);
logEventInfo.Properties["sender"] = sender;
sequence_logger.Debug(logEventInfo);
}
public static void sequence(string sender, string receiver, string message, string errDesc = "")
{
NLog.Logger sequence_logger = LogManager.GetLogger("SequenceLogger");
LogEventInfo logEventInfo = new LogEventInfo(LogLevel.Debug, "SequenceLogger", message);
logEventInfo.Properties["sender"] = sender;
logEventInfo.Properties["receiver"] = receiver;
logEventInfo.Properties["errordesc"] = (errDesc == string.Empty || errDesc.Contains("Success") == true
? $"{errDesc}"
: $"ERR-{errDesc}");
sequence_logger.Debug(logEventInfo);
}
public static string getProcessType() => m_process_type;
public static string getIpPort() => m_ip_port;
public static bool isInitialized() => m_is_initialized;
}