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 小高 閱讀(337) 評論(0)  編輯  收藏 所屬分類: DotNet

          導航

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

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類(352)

          收藏夾(19)

          關注的blog

          手冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新乡县| 海阳市| 平阴县| 云梦县| 灵璧县| 那坡县| 祁门县| 合阳县| 双峰县| 桃园县| 肇东市| 苍山县| 都江堰市| 东丽区| 盖州市| 东安县| 邵阳县| 苍山县| 呼玛县| 昭通市| 师宗县| 兴宁市| 乌兰浩特市| 海丰县| 承德县| 木里| 双牌县| 乐亭县| 邵武市| 象州县| 洮南市| 临桂县| 墨玉县| 勃利县| 泰宁县| 龙门县| 虞城县| 兴义市| 五华县| 连平县| 灵宝市|