PropertyPatternConverter.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Repository;
  25. namespace log4net.Layout.Pattern
  26. {
  27. /// <summary>
  28. /// Property pattern converter
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// Writes out the value of a named property. The property name
  33. /// should be set in the <see cref="log4net.Util.PatternConverter.Option"/>
  34. /// property.
  35. /// </para>
  36. /// <para>
  37. /// If the <see cref="log4net.Util.PatternConverter.Option"/> is set to <c>null</c>
  38. /// then all the properties are written as key value pairs.
  39. /// </para>
  40. /// </remarks>
  41. /// <author>Nicko Cadell</author>
  42. internal sealed class PropertyPatternConverter : PatternLayoutConverter
  43. {
  44. /// <summary>
  45. /// Write the property value to the output
  46. /// </summary>
  47. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  48. /// <param name="loggingEvent">the event being logged</param>
  49. /// <remarks>
  50. /// <para>
  51. /// Writes out the value of a named property. The property name
  52. /// should be set in the <see cref="log4net.Util.PatternConverter.Option"/>
  53. /// property.
  54. /// </para>
  55. /// <para>
  56. /// If the <see cref="log4net.Util.PatternConverter.Option"/> is set to <c>null</c>
  57. /// then all the properties are written as key value pairs.
  58. /// </para>
  59. /// </remarks>
  60. override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
  61. {
  62. if (Option != null)
  63. {
  64. // Write the value for the specified key
  65. WriteObject(writer, loggingEvent.Repository, loggingEvent.LookupProperty(Option));
  66. }
  67. else
  68. {
  69. // Write all the key value pairs
  70. WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
  71. }
  72. }
  73. }
  74. }