MethodItem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Text;
  21. using System.Collections;
  22. using log4net.Util;
  23. namespace log4net.Core
  24. {
  25. /// <summary>
  26. /// provides method information without actually referencing a System.Reflection.MethodBase
  27. /// as that would require that the containing assembly is loaded.
  28. /// </summary>
  29. ///
  30. #if !NETCF
  31. [Serializable]
  32. #endif
  33. public class MethodItem
  34. {
  35. #region Public Instance Constructors
  36. /// <summary>
  37. /// constructs a method item for an unknown method.
  38. /// </summary>
  39. public MethodItem()
  40. {
  41. m_name = NA;
  42. m_parameters = new string[0];
  43. }
  44. /// <summary>
  45. /// constructs a method item from the name of the method.
  46. /// </summary>
  47. /// <param name="name"></param>
  48. public MethodItem(string name)
  49. : this()
  50. {
  51. m_name = name;
  52. }
  53. /// <summary>
  54. /// constructs a method item from the name of the method and its parameters.
  55. /// </summary>
  56. /// <param name="name"></param>
  57. /// <param name="parameters"></param>
  58. public MethodItem(string name, string[] parameters)
  59. : this(name)
  60. {
  61. m_parameters = parameters;
  62. }
  63. /// <summary>
  64. /// constructs a method item from a method base by determining the method name and its parameters.
  65. /// </summary>
  66. /// <param name="methodBase"></param>
  67. public MethodItem(System.Reflection.MethodBase methodBase)
  68. : this(methodBase.Name, GetMethodParameterNames(methodBase))
  69. {
  70. }
  71. #endregion
  72. private static string[] GetMethodParameterNames(System.Reflection.MethodBase methodBase)
  73. {
  74. ArrayList methodParameterNames = new ArrayList();
  75. try
  76. {
  77. System.Reflection.ParameterInfo[] methodBaseGetParameters = methodBase.GetParameters();
  78. int methodBaseGetParametersCount = methodBaseGetParameters.GetUpperBound(0);
  79. for (int i = 0; i <= methodBaseGetParametersCount; i++)
  80. {
  81. methodParameterNames.Add(methodBaseGetParameters[i].ParameterType + " " + methodBaseGetParameters[i].Name);
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. LogLog.Error(declaringType, "An exception ocurred while retreiving method parameters.", ex);
  87. }
  88. return (string[])methodParameterNames.ToArray(typeof(string));
  89. }
  90. #region Public Instance Properties
  91. /// <summary>
  92. /// Gets the method name of the caller making the logging
  93. /// request.
  94. /// </summary>
  95. /// <value>
  96. /// The method name of the caller making the logging
  97. /// request.
  98. /// </value>
  99. /// <remarks>
  100. /// <para>
  101. /// Gets the method name of the caller making the logging
  102. /// request.
  103. /// </para>
  104. /// </remarks>
  105. public string Name
  106. {
  107. get { return m_name; }
  108. }
  109. /// <summary>
  110. /// Gets the method parameters of the caller making
  111. /// the logging request.
  112. /// </summary>
  113. /// <value>
  114. /// The method parameters of the caller making
  115. /// the logging request
  116. /// </value>
  117. /// <remarks>
  118. /// <para>
  119. /// Gets the method parameters of the caller making
  120. /// the logging request.
  121. /// </para>
  122. /// </remarks>
  123. public string[] Parameters
  124. {
  125. get { return m_parameters; }
  126. }
  127. #endregion
  128. #region Private Instance Fields
  129. private readonly string m_name;
  130. private readonly string[] m_parameters;
  131. #endregion
  132. #region Private Static Fields
  133. /// <summary>
  134. /// The fully qualified type of the StackFrameItem class.
  135. /// </summary>
  136. /// <remarks>
  137. /// Used by the internal logger to record the Type of the
  138. /// log message.
  139. /// </remarks>
  140. private readonly static Type declaringType = typeof(MethodItem);
  141. /// <summary>
  142. /// When location information is not available the constant
  143. /// <c>NA</c> is returned. Current value of this string
  144. /// constant is <b>?</b>.
  145. /// </summary>
  146. private const string NA = "?";
  147. #endregion Private Static Fields
  148. }
  149. }