EnvironmentPatternConverter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Environment.GetEnvironmentVariable()
  20. #if !NETCF
  21. using System;
  22. using System.Text;
  23. using System.IO;
  24. using log4net.Util;
  25. using log4net.DateFormatter;
  26. using log4net.Core;
  27. namespace log4net.Util.PatternStringConverters
  28. {
  29. /// <summary>
  30. /// Write an environment variable to the output
  31. /// </summary>
  32. /// <remarks>
  33. /// <para>
  34. /// Write an environment variable to the output writer.
  35. /// The value of the <see cref="log4net.Util.PatternConverter.Option"/> determines
  36. /// the name of the variable to output.
  37. /// </para>
  38. /// </remarks>
  39. /// <author>Nicko Cadell</author>
  40. internal sealed class EnvironmentPatternConverter : PatternConverter
  41. {
  42. /// <summary>
  43. /// Write an environment variable to the output
  44. /// </summary>
  45. /// <param name="writer">the writer to write to</param>
  46. /// <param name="state">null, state is not set</param>
  47. /// <remarks>
  48. /// <para>
  49. /// Writes the environment variable to the output <paramref name="writer"/>.
  50. /// The name of the environment variable to output must be set
  51. /// using the <see cref="log4net.Util.PatternConverter.Option"/>
  52. /// property.
  53. /// </para>
  54. /// </remarks>
  55. override protected void Convert(TextWriter writer, object state)
  56. {
  57. try
  58. {
  59. if (this.Option != null && this.Option.Length > 0)
  60. {
  61. // Lookup the environment variable
  62. string envValue = Environment.GetEnvironmentVariable(this.Option);
  63. #if NET_2_0
  64. // If we didn't see it for the process, try a user level variable.
  65. if (envValue == null)
  66. {
  67. envValue = Environment.GetEnvironmentVariable(this.Option, EnvironmentVariableTarget.User);
  68. }
  69. // If we still didn't find it, try a system level one.
  70. if (envValue == null)
  71. {
  72. envValue = Environment.GetEnvironmentVariable(this.Option, EnvironmentVariableTarget.Machine);
  73. }
  74. #endif
  75. if (envValue != null && envValue.Length > 0)
  76. {
  77. writer.Write(envValue);
  78. }
  79. }
  80. }
  81. catch(System.Security.SecurityException secEx)
  82. {
  83. // This security exception will occur if the caller does not have
  84. // unrestricted environment permission. If this occurs the expansion
  85. // will be skipped with the following warning message.
  86. LogLog.Debug(declaringType, "Security exception while trying to expand environment variables. Error Ignored. No Expansion.", secEx);
  87. }
  88. catch (Exception ex)
  89. {
  90. LogLog.Error(declaringType, "Error occurred while converting environment variable.", ex);
  91. }
  92. }
  93. #region Private Static Fields
  94. /// <summary>
  95. /// The fully qualified type of the EnvironmentPatternConverter class.
  96. /// </summary>
  97. /// <remarks>
  98. /// Used by the internal logger to record the Type of the
  99. /// log message.
  100. /// </remarks>
  101. private readonly static Type declaringType = typeof(EnvironmentPatternConverter);
  102. #endregion Private Static Fields
  103. }
  104. }
  105. #endif // !NETCF