NdcPatternConverter.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 log4net.Core;
  23. namespace log4net.Layout.Pattern
  24. {
  25. /// <summary>
  26. /// Converter to include event NDC
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// Outputs the value of the event property named <c>NDC</c>.
  31. /// </para>
  32. /// <para>
  33. /// The <see cref="PropertyPatternConverter"/> should be used instead.
  34. /// </para>
  35. /// </remarks>
  36. /// <author>Nicko Cadell</author>
  37. internal sealed class NdcPatternConverter : PatternLayoutConverter
  38. {
  39. /// <summary>
  40. /// Write the event NDC to the output
  41. /// </summary>
  42. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  43. /// <param name="loggingEvent">the event being logged</param>
  44. /// <remarks>
  45. /// <para>
  46. /// As the thread context stacks are now stored in named event properties
  47. /// this converter simply looks up the value of the <c>NDC</c> property.
  48. /// </para>
  49. /// <para>
  50. /// The <see cref="PropertyPatternConverter"/> should be used instead.
  51. /// </para>
  52. /// </remarks>
  53. override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
  54. {
  55. // Write the value for the specified key
  56. WriteObject(writer, loggingEvent.Repository, loggingEvent.LookupProperty("NDC"));
  57. }
  58. }
  59. }