AppSettingPatternConverter.cs 3.9 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 application .config files
  20. #if !NETCF
  21. using System;
  22. using System.Text;
  23. using System.IO;
  24. using System.Collections;
  25. using log4net.Core;
  26. using log4net.Util;
  27. using log4net.Repository;
  28. namespace log4net.Util.PatternStringConverters
  29. {
  30. /// <summary>
  31. /// AppSetting pattern converter
  32. /// </summary>
  33. /// <remarks>
  34. /// <para>
  35. /// This pattern converter reads appSettings from the application configuration file.
  36. /// </para>
  37. /// <para>
  38. /// If the <see cref="PatternConverter.Option"/> is specified then that will be used to
  39. /// lookup a single appSettings value. If no <see cref="PatternConverter.Option"/> is specified
  40. /// then all appSettings will be dumped as a list of key value pairs.
  41. /// </para>
  42. /// <para>
  43. /// A typical use is to specify a base directory for log files, e.g.
  44. /// <example>
  45. /// <![CDATA[
  46. /// <log4net>
  47. /// <appender name="MyAppender" type="log4net.Appender.RollingFileAppender">
  48. /// <file type="log4net.Util.PatternString" value="appsetting{LogDirectory}MyApp.log"/>
  49. /// ...
  50. /// </appender>
  51. /// </log4net>
  52. /// ]]>
  53. /// </example>
  54. /// </para>
  55. /// </remarks>
  56. internal sealed class AppSettingPatternConverter : PatternConverter
  57. {
  58. private static IDictionary AppSettingsDictionary
  59. {
  60. get
  61. {
  62. if (_appSettingsHashTable == null)
  63. {
  64. Hashtable h = new Hashtable();
  65. foreach(string key in System.Configuration.ConfigurationManager.AppSettings)
  66. {
  67. h.Add(key, System.Configuration.ConfigurationManager.AppSettings[key]);
  68. }
  69. _appSettingsHashTable = h;
  70. }
  71. return _appSettingsHashTable;
  72. }
  73. }
  74. private static Hashtable _appSettingsHashTable;
  75. /// <summary>
  76. /// Write the property value to the output
  77. /// </summary>
  78. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  79. /// <param name="state">null, state is not set</param>
  80. /// <remarks>
  81. /// <para>
  82. /// Writes out the value of a named property. The property name
  83. /// should be set in the <see cref="log4net.Util.PatternConverter.Option"/>
  84. /// property.
  85. /// </para>
  86. /// <para>
  87. /// If the <see cref="log4net.Util.PatternConverter.Option"/> is set to <c>null</c>
  88. /// then all the properties are written as key value pairs.
  89. /// </para>
  90. /// </remarks>
  91. override protected void Convert(TextWriter writer, object state)
  92. {
  93. if (Option != null)
  94. {
  95. // Write the value for the specified key
  96. WriteObject(writer, null, System.Configuration.ConfigurationManager.AppSettings[Option]);
  97. }
  98. else
  99. {
  100. // Write all the key value pairs
  101. WriteDictionary(writer, null, AppSettingsDictionary);
  102. }
  103. }
  104. }
  105. }
  106. #endif