EnvironmentFolderPathPatternConverter.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #if !NETCF
  20. using System;
  21. using System.IO;
  22. namespace log4net.Util.PatternStringConverters
  23. {
  24. /// <summary>
  25. /// Write an <see cref="System.Environment.SpecialFolder" /> folder path to the output
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// Write an special path environment folder path to the output writer.
  30. /// The value of the <see cref="log4net.Util.PatternConverter.Option"/> determines
  31. /// the name of the variable to output. <see cref="log4net.Util.PatternConverter.Option"/>
  32. /// should be a value in the <see cref="System.Environment.SpecialFolder" /> enumeration.
  33. /// </para>
  34. /// </remarks>
  35. /// <author>Ron Grabowski</author>
  36. internal sealed class EnvironmentFolderPathPatternConverter : PatternConverter
  37. {
  38. /// <summary>
  39. /// Write an special path environment folder path to the output
  40. /// </summary>
  41. /// <param name="writer">the writer to write to</param>
  42. /// <param name="state">null, state is not set</param>
  43. /// <remarks>
  44. /// <para>
  45. /// Writes the special path environment folder path to the output <paramref name="writer"/>.
  46. /// The name of the special path environment folder path to output must be set
  47. /// using the <see cref="log4net.Util.PatternConverter.Option"/>
  48. /// property.
  49. /// </para>
  50. /// </remarks>
  51. override protected void Convert(TextWriter writer, object state)
  52. {
  53. try
  54. {
  55. if (Option != null && Option.Length > 0)
  56. {
  57. Environment.SpecialFolder specialFolder =
  58. (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), Option, true);
  59. string envFolderPathValue = Environment.GetFolderPath(specialFolder);
  60. if (envFolderPathValue != null && envFolderPathValue.Length > 0)
  61. {
  62. writer.Write(envFolderPathValue);
  63. }
  64. }
  65. }
  66. catch (System.Security.SecurityException secEx)
  67. {
  68. // This security exception will occur if the caller does not have
  69. // unrestricted environment permission. If this occurs the expansion
  70. // will be skipped with the following warning message.
  71. LogLog.Debug(declaringType, "Security exception while trying to expand environment variables. Error Ignored. No Expansion.", secEx);
  72. }
  73. catch (Exception ex)
  74. {
  75. LogLog.Error(declaringType, "Error occurred while converting environment variable.", ex);
  76. }
  77. }
  78. #region Private Static Fields
  79. /// <summary>
  80. /// The fully qualified type of the EnvironmentFolderPathPatternConverter class.
  81. /// </summary>
  82. /// <remarks>
  83. /// Used by the internal logger to record the Type of the
  84. /// log message.
  85. /// </remarks>
  86. private readonly static Type declaringType = typeof(EnvironmentFolderPathPatternConverter);
  87. #endregion Private Static Fields
  88. }
  89. }
  90. #endif // !NETCF