AspNetContextPatternConverter.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 ASP.NET
  20. // SSCLI 1.0 has no support for ASP.NET
  21. #if !NETCF && !SSCLI && !CLIENT_PROFILE
  22. using System.IO;
  23. using System.Web;
  24. using log4net.Core;
  25. namespace log4net.Layout.Pattern
  26. {
  27. /// <summary>
  28. /// Converter for items in the <see cref="HttpContext" />.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// Outputs an item from the <see cref="HttpContext" />.
  33. /// </para>
  34. /// </remarks>
  35. /// <author>Ron Grabowski</author>
  36. internal sealed class AspNetContextPatternConverter : AspNetPatternLayoutConverter
  37. {
  38. /// <summary>
  39. /// Write the ASP.Net HttpContext item to the output
  40. /// </summary>
  41. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  42. /// <param name="loggingEvent">The <see cref="LoggingEvent" /> on which the pattern converter should be executed.</param>
  43. /// <param name="httpContext">The <see cref="HttpContext" /> under which the ASP.Net request is running.</param>
  44. /// <remarks>
  45. /// <para>
  46. /// Writes out the value of a named property. The property name
  47. /// should be set in the <see cref="log4net.Util.PatternConverter.Option"/>
  48. /// property.
  49. /// </para>
  50. /// </remarks>
  51. protected override void Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext)
  52. {
  53. if (Option != null)
  54. {
  55. WriteObject(writer, loggingEvent.Repository, httpContext.Items[Option]);
  56. }
  57. else
  58. {
  59. WriteObject(writer, loggingEvent.Repository, httpContext.Items);
  60. }
  61. }
  62. }
  63. }
  64. #endif