초기커밋
This commit is contained in:
55
ServerCore/Log/CloudWatchLog.cs
Normal file
55
ServerCore/Log/CloudWatchLog.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Amazon.Runtime;
|
||||
|
||||
|
||||
using NLog;
|
||||
using NLog.AWS.Logger;
|
||||
using NLog.Config;
|
||||
using NLog.Layouts;
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
public static class CloudWatchLog
|
||||
{
|
||||
private static bool m_is_opened = true;
|
||||
|
||||
public static bool setup( string? logGroup = null, string? logNamePattern = null, string? logLevel = null
|
||||
, string? awsRegion = null, string? awsAccessKey = null, string? awsSecretKey = null
|
||||
, Layout? layout = null )
|
||||
{
|
||||
if ( logGroup == string.Empty ||
|
||||
logNamePattern == string.Empty ||
|
||||
logLevel == string.Empty ||
|
||||
awsRegion == string.Empty ||
|
||||
awsAccessKey == string.Empty ||
|
||||
awsSecretKey == string.Empty )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var current_config = LogManager.Configuration;
|
||||
if(null == current_config)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var aws_target = new AWSTarget()
|
||||
{
|
||||
LogGroup = logGroup,
|
||||
Region = awsRegion,
|
||||
Credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey),
|
||||
Layout = layout
|
||||
};
|
||||
|
||||
var newRule = new LoggingRule(logNamePattern, LogLevel.FromString(logLevel), aws_target);
|
||||
current_config.LoggingRules.Add(newRule);
|
||||
|
||||
LogManager.ReconfigExistingLoggers();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void setClosed() => m_is_opened = false;
|
||||
|
||||
public static bool isOpened() => m_is_opened;
|
||||
}
|
||||
224
ServerCore/Log/Logger.cs
Normal file
224
ServerCore/Log/Logger.cs
Normal file
@@ -0,0 +1,224 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using NLog;
|
||||
using NLog.Config;
|
||||
using NLog.Fluent;
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
//===========================================================================================
|
||||
// 로그 처리자
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
|
||||
//===========================================================================================
|
||||
// 로그 관련 확장 함수
|
||||
//
|
||||
// author : kangms
|
||||
//
|
||||
//===========================================================================================
|
||||
|
||||
public static class NLogExtend
|
||||
{
|
||||
|
||||
public static void trace( this NLog.Logger _this
|
||||
, string message
|
||||
, [CallerMemberName] string memberName = ""
|
||||
, [CallerFilePath] string sourceFilePath = ""
|
||||
, [CallerLineNumber] Int32 sourceLineNumber = 0 )
|
||||
{
|
||||
var logEventInfo = new LogEventInfo(LogLevel.Trace, null, message);
|
||||
logEventInfo.setLogEventProperties( sourceFilePath, sourceLineNumber, memberName
|
||||
, Log.getProcessType(), Log.getIpPort() );
|
||||
|
||||
_this.Trace(logEventInfo);
|
||||
}
|
||||
|
||||
public static void debug( this NLog.Logger _this
|
||||
, string message
|
||||
, [CallerMemberName] string memberName = ""
|
||||
, [CallerFilePath] string sourceFilePath = ""
|
||||
, [CallerLineNumber] Int32 sourceLineNumber = 0 )
|
||||
{
|
||||
var logEventInfo = new LogEventInfo(LogLevel.Debug, null, message);
|
||||
logEventInfo.setLogEventProperties( sourceFilePath, sourceLineNumber, memberName
|
||||
, Log.getProcessType(), Log.getIpPort() );
|
||||
|
||||
_this.Debug(logEventInfo);
|
||||
}
|
||||
|
||||
public static void info( this NLog.Logger _this
|
||||
, string message
|
||||
, [CallerMemberName] string memberName = ""
|
||||
, [CallerFilePath] string sourceFilePath = ""
|
||||
, [CallerLineNumber] Int32 sourceLineNumber = 0 )
|
||||
{
|
||||
var logEventInfo = new LogEventInfo(LogLevel.Info, null, message);
|
||||
logEventInfo.setLogEventProperties( sourceFilePath, sourceLineNumber, memberName
|
||||
, Log.getProcessType(), Log.getIpPort() );
|
||||
|
||||
_this.Info(logEventInfo);
|
||||
}
|
||||
|
||||
public static void warn( this NLog.Logger _this
|
||||
, string message
|
||||
, [CallerMemberName] string memberName = ""
|
||||
, [CallerFilePath] string sourceFilePath = ""
|
||||
, [CallerLineNumber] Int32 sourceLineNumber = 0 )
|
||||
{
|
||||
var logEventInfo = new LogEventInfo(LogLevel.Warn, null, message);
|
||||
logEventInfo.setLogEventProperties( sourceFilePath, sourceLineNumber, memberName
|
||||
, Log.getProcessType(), Log.getIpPort() );
|
||||
|
||||
_this.Warn(logEventInfo);
|
||||
}
|
||||
|
||||
public static void error( this NLog.Logger _this
|
||||
, string message
|
||||
, [CallerMemberName] string memberName = ""
|
||||
, [CallerFilePath] string sourceFilePath = ""
|
||||
, [CallerLineNumber] Int32 sourceLineNumber = 0 )
|
||||
{
|
||||
var logEventInfo = new LogEventInfo(LogLevel.Error, null, message);
|
||||
logEventInfo.setLogEventProperties( sourceFilePath, sourceLineNumber, memberName
|
||||
, Log.getProcessType(), Log.getIpPort() );
|
||||
|
||||
_this.Error(logEventInfo);
|
||||
}
|
||||
|
||||
public static void fatal( this NLog.Logger _this
|
||||
, string message
|
||||
, [CallerMemberName] string memberName = ""
|
||||
, [CallerFilePath] string sourceFilePath = ""
|
||||
, [CallerLineNumber] Int32 sourceLineNumber = 0 )
|
||||
{
|
||||
var logEventInfo = new LogEventInfo(LogLevel.Fatal, null, message);
|
||||
logEventInfo.setLogEventProperties( sourceFilePath, sourceLineNumber, memberName
|
||||
, Log.getProcessType(), Log.getIpPort() );
|
||||
|
||||
_this.Fatal(logEventInfo);
|
||||
}
|
||||
|
||||
private static void setLogEventProperties( this LogEventInfo _this
|
||||
, string sourceFilePath, Int32 sourceLineNumber, string memberName
|
||||
, string processType, string ipPort )
|
||||
{
|
||||
_this.Properties["server"] = processType;
|
||||
_this.Properties["ip/port"] = ipPort;
|
||||
_this.Properties["memberName"] = memberName;
|
||||
_this.Properties["filePath"] = sourceFilePath;
|
||||
_this.Properties["lineNumber"] = sourceLineNumber;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user