ExceptionLayout.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.IO;
  21. using System.Text;
  22. using log4net.Util;
  23. using log4net.Core;
  24. namespace log4net.Layout
  25. {
  26. /// <summary>
  27. /// A Layout that renders only the Exception text from the logging event
  28. /// </summary>
  29. /// <remarks>
  30. /// <para>
  31. /// A Layout that renders only the Exception text from the logging event.
  32. /// </para>
  33. /// <para>
  34. /// This Layout should only be used with appenders that utilize multiple
  35. /// layouts (e.g. <see cref="log4net.Appender.AdoNetAppender"/>).
  36. /// </para>
  37. /// </remarks>
  38. /// <author>Nicko Cadell</author>
  39. /// <author>Gert Driesen</author>
  40. public class ExceptionLayout : LayoutSkeleton
  41. {
  42. #region Constructors
  43. /// <summary>
  44. /// Default constructor
  45. /// </summary>
  46. /// <remarks>
  47. /// <para>
  48. /// Constructs a ExceptionLayout
  49. /// </para>
  50. /// </remarks>
  51. public ExceptionLayout()
  52. {
  53. this.IgnoresException = false;
  54. }
  55. #endregion
  56. #region Implementation of IOptionHandler
  57. /// <summary>
  58. /// Activate component options
  59. /// </summary>
  60. /// <remarks>
  61. /// <para>
  62. /// Part of the <see cref="IOptionHandler"/> component activation
  63. /// framework.
  64. /// </para>
  65. /// <para>
  66. /// This method does nothing as options become effective immediately.
  67. /// </para>
  68. /// </remarks>
  69. override public void ActivateOptions()
  70. {
  71. // nothing to do.
  72. }
  73. #endregion
  74. #region Override implementation of LayoutSkeleton
  75. /// <summary>
  76. /// Gets the exception text from the logging event
  77. /// </summary>
  78. /// <param name="writer">The TextWriter to write the formatted event to</param>
  79. /// <param name="loggingEvent">the event being logged</param>
  80. /// <remarks>
  81. /// <para>
  82. /// Write the exception string to the <see cref="TextWriter"/>.
  83. /// The exception string is retrieved from <see cref="M:LoggingEvent.GetExceptionString()"/>.
  84. /// </para>
  85. /// </remarks>
  86. override public void Format(TextWriter writer, LoggingEvent loggingEvent)
  87. {
  88. if (loggingEvent == null)
  89. {
  90. throw new ArgumentNullException("loggingEvent");
  91. }
  92. writer.Write(loggingEvent.GetExceptionString());
  93. }
  94. #endregion
  95. }
  96. }