PropertyPatternConverter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.IO;
  22. using System.Collections;
  23. using log4net.Core;
  24. using log4net.Util;
  25. using log4net.Repository;
  26. namespace log4net.Util.PatternStringConverters
  27. {
  28. /// <summary>
  29. /// Property pattern converter
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>
  33. /// This pattern converter reads the thread and global properties.
  34. /// The thread properties take priority over global properties.
  35. /// See <see cref="ThreadContext.Properties"/> for details of the
  36. /// thread properties. See <see cref="GlobalContext.Properties"/> for
  37. /// details of the global properties.
  38. /// </para>
  39. /// <para>
  40. /// If the <see cref="PatternConverter.Option"/> is specified then that will be used to
  41. /// lookup a single property. If no <see cref="PatternConverter.Option"/> is specified
  42. /// then all properties will be dumped as a list of key value pairs.
  43. /// </para>
  44. /// </remarks>
  45. /// <author>Nicko Cadell</author>
  46. internal sealed class PropertyPatternConverter : PatternConverter
  47. {
  48. /// <summary>
  49. /// Write the property value to the output
  50. /// </summary>
  51. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  52. /// <param name="state">null, state is not set</param>
  53. /// <remarks>
  54. /// <para>
  55. /// Writes out the value of a named property. The property name
  56. /// should be set in the <see cref="log4net.Util.PatternConverter.Option"/>
  57. /// property.
  58. /// </para>
  59. /// <para>
  60. /// If the <see cref="log4net.Util.PatternConverter.Option"/> is set to <c>null</c>
  61. /// then all the properties are written as key value pairs.
  62. /// </para>
  63. /// </remarks>
  64. override protected void Convert(TextWriter writer, object state)
  65. {
  66. CompositeProperties compositeProperties = new CompositeProperties();
  67. #if !NETCF
  68. PropertiesDictionary logicalThreadProperties = LogicalThreadContext.Properties.GetProperties(false);
  69. if (logicalThreadProperties != null)
  70. {
  71. compositeProperties.Add(logicalThreadProperties);
  72. }
  73. #endif
  74. PropertiesDictionary threadProperties = ThreadContext.Properties.GetProperties(false);
  75. if (threadProperties != null)
  76. {
  77. compositeProperties.Add(threadProperties);
  78. }
  79. // TODO: Add Repository Properties
  80. compositeProperties.Add(GlobalContext.Properties.GetReadOnlyProperties());
  81. if (Option != null)
  82. {
  83. // Write the value for the specified key
  84. WriteObject(writer, null, compositeProperties[Option]);
  85. }
  86. else
  87. {
  88. // Write all the key value pairs
  89. WriteDictionary(writer, null, compositeProperties.Flatten());
  90. }
  91. }
  92. }
  93. }