DevLog 클래스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Threading;
namespace csharp_test_client
{
public class DevLog
{
static System.Collections.Concurrent.ConcurrentQueue<string> logMsgQueue = new System.Collections.Concurrent.ConcurrentQueue<string>();
static Int64 출력가능_로그레벨 = (Int64)LOG_LEVEL.TRACE;
- logMsgQueue : ConcurrentQueue 클래스를 사용한 스레드 간에 안전하게 메시지를 전달하기 위한 큐 를 정적으로 선언함
- ConcurrentQueue : .NET에서 제공하는 스레드용 큐
- 출력가능_로그레벨 : 로그레벨 상수
- 로그 메시지의 중요도 : TRACE는 가장 낮은 중요도로, 디버깅 정보 포함
Init
static public void Init(LOG_LEVEL logLevel)
{
ChangeLogLevel(logLevel);
}
ChangeLogLevel
static public void ChangeLogLevel(LOG_LEVEL logLevel)
{
Interlocked.Exchange(ref 출력가능_로그레벨, (int)logLevel);
}
- 멀티스레드 환경에서 안전하게 변수의 값을 조작하는 Interlocked 사용
- 전에 선언한 출력가능_로그레벨 변수의 값을 변경하는 함수
CurrentLogLevel
public static LOG_LEVEL CurrentLogLevel()
{
var curLogLevel = (LOG_LEVEL)Interlocked.Read(ref 출력가능_로그레벨);
return curLogLevel;
}
- 멀티스레드 환경에서 동기화 메서드인 Read 사용
- 출력가능_로그레벨 변수 값을 가져와서 LOG_LEVEL 형으로 바꾼 뒤 반환
- 현재 로그 레벨을 알려주는 함수
Write
static public void Write(string msg, LOG_LEVEL logLevel = LOG_LEVEL.TRACE,
[CallerFilePath] string fileName = "",
[CallerMemberName] string methodName = "",
[CallerLineNumber] int lineNumber = 0)
{
if (CurrentLogLevel() <= logLevel)
{
logMsgQueue.Enqueue(string.Format("{0}:{1}| {2}", DateTime.Now, methodName, msg));
}
}