StackFrameItem.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #if !NETCF
  2. #region Apache License
  3. //
  4. // Licensed to the Apache Software Foundation (ASF) under one or more
  5. // contributor license agreements. See the NOTICE file distributed with
  6. // this work for additional information regarding copyright ownership.
  7. // The ASF licenses this file to you under the Apache License, Version 2.0
  8. // (the "License"); you may not use this file except in compliance with
  9. // the License. You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. //
  19. #endregion
  20. using System;
  21. using System.Text;
  22. using System.Diagnostics;
  23. using System.Reflection;
  24. using log4net.Util;
  25. namespace log4net.Core
  26. {
  27. /// <summary>
  28. /// provides stack frame information without actually referencing a System.Diagnostics.StackFrame
  29. /// as that would require that the containing assembly is loaded.
  30. /// </summary>
  31. ///
  32. [Serializable]
  33. public class StackFrameItem
  34. {
  35. #region Public Instance Constructors
  36. /// <summary>
  37. /// returns a stack frame item from a stack frame. This
  38. /// </summary>
  39. /// <param name="frame"></param>
  40. /// <returns></returns>
  41. public StackFrameItem(StackFrame frame)
  42. {
  43. // set default values
  44. m_lineNumber = NA;
  45. m_fileName = NA;
  46. m_method = new MethodItem();
  47. m_className = NA;
  48. try
  49. {
  50. // get frame values
  51. m_lineNumber = frame.GetFileLineNumber().ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
  52. m_fileName = frame.GetFileName();
  53. // get method values
  54. MethodBase method = frame.GetMethod();
  55. if (method != null)
  56. {
  57. if(method.DeclaringType != null)
  58. m_className = method.DeclaringType.FullName;
  59. m_method = new MethodItem(method);
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. LogLog.Error(declaringType, "An exception ocurred while retreiving stack frame information.", ex);
  65. }
  66. // set full info
  67. m_fullInfo = m_className + '.' + m_method.Name + '(' + m_fileName + ':' + m_lineNumber + ')';
  68. }
  69. #endregion
  70. #region Public Instance Properties
  71. /// <summary>
  72. /// Gets the fully qualified class name of the caller making the logging
  73. /// request.
  74. /// </summary>
  75. /// <value>
  76. /// The fully qualified class name of the caller making the logging
  77. /// request.
  78. /// </value>
  79. /// <remarks>
  80. /// <para>
  81. /// Gets the fully qualified class name of the caller making the logging
  82. /// request.
  83. /// </para>
  84. /// </remarks>
  85. public string ClassName
  86. {
  87. get { return m_className; }
  88. }
  89. /// <summary>
  90. /// Gets the file name of the caller.
  91. /// </summary>
  92. /// <value>
  93. /// The file name of the caller.
  94. /// </value>
  95. /// <remarks>
  96. /// <para>
  97. /// Gets the file name of the caller.
  98. /// </para>
  99. /// </remarks>
  100. public string FileName
  101. {
  102. get { return m_fileName; }
  103. }
  104. /// <summary>
  105. /// Gets the line number of the caller.
  106. /// </summary>
  107. /// <value>
  108. /// The line number of the caller.
  109. /// </value>
  110. /// <remarks>
  111. /// <para>
  112. /// Gets the line number of the caller.
  113. /// </para>
  114. /// </remarks>
  115. public string LineNumber
  116. {
  117. get { return m_lineNumber; }
  118. }
  119. /// <summary>
  120. /// Gets the method name of the caller.
  121. /// </summary>
  122. /// <value>
  123. /// The method name of the caller.
  124. /// </value>
  125. /// <remarks>
  126. /// <para>
  127. /// Gets the method name of the caller.
  128. /// </para>
  129. /// </remarks>
  130. public MethodItem Method
  131. {
  132. get { return m_method; }
  133. }
  134. /// <summary>
  135. /// Gets all available caller information
  136. /// </summary>
  137. /// <value>
  138. /// All available caller information, in the format
  139. /// <c>fully.qualified.classname.of.caller.methodName(Filename:line)</c>
  140. /// </value>
  141. /// <remarks>
  142. /// <para>
  143. /// Gets all available caller information, in the format
  144. /// <c>fully.qualified.classname.of.caller.methodName(Filename:line)</c>
  145. /// </para>
  146. /// </remarks>
  147. public string FullInfo
  148. {
  149. get { return m_fullInfo; }
  150. }
  151. #endregion Public Instance Properties
  152. #region Private Instance Fields
  153. private readonly string m_lineNumber;
  154. private readonly string m_fileName;
  155. private readonly string m_className;
  156. private readonly string m_fullInfo;
  157. private readonly MethodItem m_method;
  158. #endregion
  159. #region Private Static Fields
  160. /// <summary>
  161. /// The fully qualified type of the StackFrameItem class.
  162. /// </summary>
  163. /// <remarks>
  164. /// Used by the internal logger to record the Type of the
  165. /// log message.
  166. /// </remarks>
  167. private readonly static Type declaringType = typeof(StackFrameItem);
  168. /// <summary>
  169. /// When location information is not available the constant
  170. /// <c>NA</c> is returned. Current value of this string
  171. /// constant is <b>?</b>.
  172. /// </summary>
  173. private const string NA = "?";
  174. #endregion Private Static Fields
  175. }
  176. }
  177. #endif