JUST DO IT ~

我只想当个程序员

log4 net 出错 定位行 的能力 LocationInfo using System.Diagnostics;


片段 "


 public LocationInfo(Type callerStackBoundaryDeclaringType)
  {
   // Initialize all fields
   m_className = NA;
   m_fileName = NA;
   m_lineNumber = NA;
   m_methodName = NA;
   m_fullInfo = NA;

#if !NETCF
   if (callerStackBoundaryDeclaringType != null)
   {
    try
    {
     StackTrace st = new StackTrace(true);
     int frameIndex = 0;

     // skip frames not from fqnOfCallingClass
     while (frameIndex < st.FrameCount)
     {
      StackFrame frame = st.GetFrame(frameIndex);
      if (frame != null && frame.GetMethod().DeclaringType == callerStackBoundaryDeclaringType)
      {
       break;
      }
      frameIndex++;
     }

     // skip frames from fqnOfCallingClass
     while (frameIndex < st.FrameCount)
     {
      StackFrame frame = st.GetFrame(frameIndex);
      if (frame != null && frame.GetMethod().DeclaringType != callerStackBoundaryDeclaringType)
      {
       break;
      }
      frameIndex++;
     }

     if (frameIndex < st.FrameCount)
     {
      // now frameIndex is the first 'user' caller frame
      StackFrame locationFrame = st.GetFrame(frameIndex);

      if (locationFrame != null)
      {
       System.Reflection.MethodBase method = locationFrame.GetMethod();

       if (method != null)
       {
        m_methodName =  method.Name;
        if (method.DeclaringType != null)
        {
         m_className = method.DeclaringType.FullName;
        }
       }
       m_fileName = locationFrame.GetFileName();
       m_lineNumber = locationFrame.GetFileLineNumber().ToString(System.Globalization.NumberFormatInfo.InvariantInfo);

       // Combine all location info
       m_fullInfo =  m_className + '.' + m_methodName + '(' + m_fileName + ':' + m_lineNumber + ')';

      }
     }
    }
    catch(System.Security.SecurityException)
    {
     // This security exception will occur if the caller does not have
     // some undefined set of SecurityPermission flags.
     LogLog.Debug("LocationInfo: Security exception while trying to get caller stack frame. Error Ignored. Location Information Not Available.");
    }
   }
#endif
  }





posted on 2009-10-01 00:02 小高 阅读(318) 评论(0)  编辑  收藏 所属分类: DotNet


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(3)

随笔分类(352)

收藏夹(19)

关注的blog

手册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜