Log4Net ã®ã©ããã¼ãã¤ãã
åå¿é²ã®æå³ãè¾¼ãã¦ã
ãããããã¨ã¯ã
- Debug ç³»ã¡ã½ããã¯ãªãªã¼ã¹ã¢ã¼ãã§ã¯å¼åºããã¨åé¤ããã
- ãã¡ãã¡
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
ã£ã¦æ¸ãã®ã ããããçç¥ããã
ã® 2 ã¤ã
Debug ç³»ã®ã¡ã½ããããªãªã¼ã¹ã¢ã¼ãã§å¼åºããã¨åé¤ãã
ããã¯ãConditionalAttribute ã使ãã°åºæ¥ããã ã¨è»½ãèãã¦ãããã ãã©ããã®å±æ§ãã¤ã³ã¿ã¼ãã§ã¤ã¹ããæ½è±¡ã¡ã½ããããã«ã¯æå®ã§ããªããããã»ã»ã»
ã¾ããå½ããåã£ã¡ããå½ããåãªãã ãã©ã»ã»ã»
ã§ãpartial ã¡ã½ããããªããããã£ã½ããã¨ã«ä½¿ãããã»ã»ã»ã¨ãæã£ããã ãã©ããã£ã¡ãå¶éãå³ãããã¦å®ç¾ä¸å¯è½ã»ã»ã»
ãããå½ããåãªãã ãã©ãã»ã»ã»
ã§ãçµå±ãObsoluteAttribute 㨠pragma ã¨æ¡å¼µã¡ã½ãããçµã¿åããããã¨ã«ã
以ä¸å®è£
ä¾ã
public interface ILogger { void Error(object message, Exception e); void Error(object message); void ErrorFormat(string format, params object[] args); // Debug以å¤ã®ã¡ã½ãã // ... // Debugç³»ã®ã¡ã½ããã«ã¯ObsoluteAttributeãä»ãã // ããã«ååããããã«ã使ã£ã¡ãé§ç®ããããªã¡ã½ããã«ã [Obsolute] void XXX_Debug(object message, Exception e); [Obsolute] void XXX_Debug(object message); [Obsolute] void XXX_DebugFormat(string format, params object[] args); } // Obsoluteãªã¡ã½ããã使ç¨ããéã®è¦åãä¸æçã«ç¡å¹ã«ã #pragma warning disable 612 public static class ILoggerExtension { // æ¡å¼µã¡ã½ããã¯ãã ã®staticã¡ã½ãããªã®ã§ãConditionalAttributeãæå®ã§ãã [Conditional("DEBUG")] public static void Debug(this ILogger self, object message, Exception e) { self.XXX_Debug(message, e); } [Conditional("DEBUG")] public static void Debug(this ILogger self, object message) { self.XXX_Debug(message); } [Conditional("DEBUG")] public static void DebugFormat(this ILogger self, string format, params object[] args) { self.XXX_DebugFormat(format, args); } } #pragma warning restore 612
// ãªãã®é¢ç½ã¿ããªãå®è£ ã¯ã©ã¹ internal sealed class Logger : ILogger { internal readonly log4net.ILog logger; // 以ä¸ILoggerã®åã¡ã½ãããLog4Netã®åã¡ã½ããã«è»¢é // ... }
ããã§ãä¾ãã°ãããªå¼åºã
log.Debug(Hoge());
ã¯ããããã°ã¢ã¼ãã§ãã«ãããå ´å㯠Hoge ã¡ã½ãããå¼ã³åºããããã®çµæããã°ã«æ¸ãè¾¼ã¾ãããã©ããªãªã¼ã¹ã¢ã¼ãã§ãã«ãããå ´å㯠Hoge ã¡ã½ããã®å¼åºããããªããªãã
ãªã®ã§ãDebug ç³»ã®ã¡ã½ããã§ã¯å¹çãæ°ã«ããã«æååçµåããã¾ãã£ãããã¦ãå
¨ç¶å¹³æ°ï¼
XXX_Debug ããå¤ãªã¡ã½ãããæ°ã«ãªããã©ãããã使ã£ããè¦ååºãããããã¯å¦¥åããã¨è¨ããã¨ã§ã
LogManager.GetLogger ã«èªåã§èªèº«ã® Type ãªãã¸ã§ã¯ãã渡ã
çã® Log4Net ã®å ´åããã¬ã¼ãåå¾ããããã«ã¯
static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
ãããªæãã®ã³ã¼ãã«ãªãã
ãªãã¸ãã¤ã«ãã¦ã¯é·ããããSystem.Reflection ã using ããã®ã¯ã»ã»ã»ã¨ããå ´åãæ´ã«é·ãã
static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
ãããã«ããããã¡ãã¡æ¸ãã®ã¯ã ããã»ã»ã»ããï¼
ã¨ãããã¨ã§ãJIT 最適化にも負けずに呼び出し元のメソッドを取得する方法ãåèã« (ã¨ããããã®ã¾ã¾)ããããªå®è£ ã
public static class LoggerPool { [DynamicSecurityMethod] public static ILogger GetLogger() { const int callerFrameIndex = 1; StackFrame callerFrame = new StackFrame(callerFrameIndex); MethodBase callerMethod = callerFrame.GetMethod(); return new Logger(LogManager.GetLogger(callerMethod.DeclaringType)); } } namespace System.Security { [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)] internal sealed class DynamicSecurityMethodAttribute : Attribute { } }
ããã§ããã¬ã¼ã®åå¾ã¯
static readonly ILogger logger = LoggerPool.GetLogger();
ããã ãã
ãªãã±ï¼æ¡å¼µã¡ã½ããã§ã¢ããªã±ã¼ã·ã§ã³ç¬èªã®æ©è½ã追å ãã
ä¾ãã°ãã¢ããªã±ã¼ã·ã§ã³ã«ãã£ã¦ã¯ãã¦ã¼ã¶ã¼ ID ããã°ã«å«ããããã ï¼ãã£ã¦è¦ä»¶ã¨ããããã¨æãã
ãããªå ´åã§ããæ¡å¼µã¡ã½ããã使ãã°ç°¡åã«ãããå®ç¾ã§ããã
public static void ErrorWithUID(this ILogger self, object message) { var uid = // ã¦ã¼ã¶ã¼IDãåå¾ self.ErrorFormat("UID:{0}, MESSAGE:{1}", uid, message); }
ãªã©ãªã©ãæ¯è¼çç°¡åã«ç¬èªæ©è½ã追å ã§ããã