ConfiguratorAttribute.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // .NET Compact Framework 1.0 has no support for reading assembly attributes
  20. #if !NETCF
  21. using System;
  22. using System.Reflection;
  23. using log4net.Repository;
  24. namespace log4net.Config
  25. {
  26. /// <summary>
  27. /// Base class for all log4net configuration attributes.
  28. /// </summary>
  29. /// <remarks>
  30. /// This is an abstract class that must be extended by
  31. /// specific configurators. This attribute allows the
  32. /// configurator to be parameterized by an assembly level
  33. /// attribute.
  34. /// </remarks>
  35. /// <author>Nicko Cadell</author>
  36. /// <author>Gert Driesen</author>
  37. [AttributeUsage(AttributeTargets.Assembly)]
  38. public abstract class ConfiguratorAttribute : Attribute, IComparable
  39. {
  40. private int m_priority = 0;
  41. /// <summary>
  42. /// Constructor used by subclasses.
  43. /// </summary>
  44. /// <param name="priority">the ordering priority for this configurator</param>
  45. /// <remarks>
  46. /// <para>
  47. /// The <paramref name="priority"/> is used to order the configurator
  48. /// attributes before they are invoked. Higher priority configurators are executed
  49. /// before lower priority ones.
  50. /// </para>
  51. /// </remarks>
  52. protected ConfiguratorAttribute(int priority)
  53. {
  54. m_priority = priority;
  55. }
  56. /// <summary>
  57. /// Configures the <see cref="ILoggerRepository"/> for the specified assembly.
  58. /// </summary>
  59. /// <param name="sourceAssembly">The assembly that this attribute was defined on.</param>
  60. /// <param name="targetRepository">The repository to configure.</param>
  61. /// <remarks>
  62. /// <para>
  63. /// Abstract method implemented by a subclass. When this method is called
  64. /// the subclass should configure the <paramref name="targetRepository"/>.
  65. /// </para>
  66. /// </remarks>
  67. public abstract void Configure(Assembly sourceAssembly, ILoggerRepository targetRepository);
  68. /// <summary>
  69. /// Compare this instance to another ConfiguratorAttribute
  70. /// </summary>
  71. /// <param name="obj">the object to compare to</param>
  72. /// <returns>see <see cref="IComparable.CompareTo"/></returns>
  73. /// <remarks>
  74. /// <para>
  75. /// Compares the priorities of the two <see cref="ConfiguratorAttribute"/> instances.
  76. /// Sorts by priority in descending order. Objects with the same priority are
  77. /// randomly ordered.
  78. /// </para>
  79. /// </remarks>
  80. public int CompareTo(object obj)
  81. {
  82. // Reference equals
  83. if ((object)this == obj)
  84. {
  85. return 0;
  86. }
  87. int result = -1;
  88. ConfiguratorAttribute target = obj as ConfiguratorAttribute;
  89. if (target != null)
  90. {
  91. // Compare the priorities
  92. result = target.m_priority.CompareTo(m_priority);
  93. if (result == 0)
  94. {
  95. // Same priority, so have to provide some ordering
  96. result = -1;
  97. }
  98. }
  99. return result;
  100. }
  101. }
  102. }
  103. #endif //!NETCF