앞의 포스트에서 log4net의 동적 설정에 대해서 이런저런 이야기를 펼쳤다.
이 기준을 가지고 몇 가지 수정하면서 간단한 설정용 클래스를 만들었다.
물론 Rolling File에 대한 설정만 되는 간단한 구조이기 때문에 추후 지속적인 업데이트가 필요하다.
다만, 프로젝트들을 하면서 매번 코드 덩어리를 붙여넣기에 한계가 있어, 아예 클래스로 구성해봤다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Text.RegularExpressions; namespace net.knoie { public class LoggerSettings { private string m_sLogFullFilename = string.Empty; public static LoggerSettings Current { get; private set; } public static void CreateInstance(string sLogFullFilename) { Current = new LoggerSettings(sLogFullFilename); } private LoggerSettings(string sLogFullFilename) { m_sLogFullFilename = sLogFullFilename; createLoggerSettings(); } public int LogLevel { get { if (CurLogger.Level == log4net.Core.Level.Alert) return 1; if (CurLogger.Level == log4net.Core.Level.All) return 2; if (CurLogger.Level == log4net.Core.Level.Critical) return 3; if (CurLogger.Level == log4net.Core.Level.Debug) return 4; if (CurLogger.Level == log4net.Core.Level.Emergency) return 5; if (CurLogger.Level == log4net.Core.Level.Error) return 6; if (CurLogger.Level == log4net.Core.Level.Fatal ) return 7; if (CurLogger.Level == log4net.Core.Level.Fine ) return 8; if (CurLogger.Level == log4net.Core.Level.Finer ) return 9; if (CurLogger.Level == log4net.Core.Level.Finest ) return 10; if (CurLogger.Level == log4net.Core.Level.Info ) return 11; if (CurLogger.Level == log4net.Core.Level.Log4Net_Debug ) return 12; if (CurLogger.Level == log4net.Core.Level.Notice ) return 13; if (CurLogger.Level == log4net.Core.Level.Off ) return 14; if (CurLogger.Level == log4net.Core.Level.Severe ) return 15; if (CurLogger.Level == log4net.Core.Level.Trace ) return 16; if (CurLogger.Level == log4net.Core.Level.Verbose) return 17; if (CurLogger.Level == log4net.Core.Level.Warn) return 18; return 14; } set { switch (value) { case 0: this.Level = log4net.Core.Level.Off; break; case 1: this.Level = log4net.Core.Level.Alert; break; case 2: this.Level = log4net.Core.Level.All; break; case 3: this.Level = log4net.Core.Level.Critical; break; case 4: this.Level = log4net.Core.Level.Debug; break; case 5: this.Level = log4net.Core.Level.Emergency; break; case 6: this.Level = log4net.Core.Level.Error; break; case 7: this.Level = log4net.Core.Level.Fatal; break; case 8: this.Level = log4net.Core.Level.Fine; break; case 9: this.Level = log4net.Core.Level.Finer; break; case 10: this.Level = log4net.Core.Level.Finest; break; case 11: this.Level = log4net.Core.Level.Info; break; case 12: this.Level = log4net.Core.Level.Log4Net_Debug; break; case 13: this.Level = log4net.Core.Level.Notice; break; case 14: this.Level = log4net.Core.Level.Off; break; case 15: this.Level = log4net.Core.Level.Severe; break; case 16: this.Level = log4net.Core.Level.Trace; break; case 17: this.Level = log4net.Core.Level.Verbose; break; case 18: this.Level = log4net.Core.Level.Warn; break; } } } public log4net.Core.Level Level { get { return CurLogger.Level; } set { CurLogger.Level = value; } } public bool EnableRollingAppender { get { foreach (log4net.Appender.AppenderSkeleton appender in CurLogger.Appenders) { if (appender is log4net.Appender.RollingFileAppender) return true; } return false; } set { if (value) { if (this.EnableRollingAppender == false) { string sLogPath = m_sLogFullFilename; log4net.Appender.RollingFileAppender rollingAppender = CreateRollingFileAppender(sLogPath); CurLogger.AddAppender(rollingAppender); rollingAppender.ActivateOptions(); } } else { if (this.EnableRollingAppender) { log4net.Appender.RollingFileAppender rollingAppender = null; foreach (log4net.Appender.IAppender appender in CurLogger.Appenders) { if (appender is log4net.Appender.RollingFileAppender) { rollingAppender = (log4net.Appender.RollingFileAppender)appender; break; } } if (rollingAppender != null) CurLogger.RemoveAppender(rollingAppender); } } } } private log4net.Repository.Hierarchy.Logger CurLogger { get { log4net.Repository.Hierarchy.Hierarchy hierarchy = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository(); return hierarchy.Root; } } private void createLoggerSettings() { try { log4net.Repository.Hierarchy.Hierarchy hierarchy = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository(); hierarchy.Configured = true; } catch { } } private log4net.Appender.RollingFileAppender CreateRollingFileAppender(string sLogPath) { log4net.Appender.RollingFileAppender rollingAppender = new log4net.Appender.RollingFileAppender(); rollingAppender.File = sLogPath; rollingAppender.AppendToFile = true; rollingAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date; rollingAppender.LockingModel = new log4net.Appender.FileAppender.MinimalLock(); rollingAppender.DatePattern = "_yyyyMMdd\".log\""; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%date %-5level %logger - %message%newline"); rollingAppender.Layout = layout; return rollingAppender; } } }
이 코드를 사용하려면, log를 사용하기 전에 실행되는 위치에 다음과 같은 코드를 넣어주어야 한다.
string sLogPath = "C:\Temp\this.log"; net.knoie.LoggerSettings.CreateInstance(sLogPath); net.knoie.LoggerSettings.Current.EnableRollingAppender = true; net.knoie.LoggerSettings.Current.Level = log4net.Core.Level.All;
sLogPath 라는 부분이 있는데, 이 안에 로그파일이 생성되어야 할 위치를 설정해 주면 된다.
이 클래스와 테스트 프로그램은 첨부파일로 같이 첨부한다.
( 첨부된 예제는 Visual Studio 2010 으로 작성된 WinForm Application 이다.)
728x90