AspNetRequestPatternConverter.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. using log4net.Util;
  26. namespace log4net.Layout.Pattern
  27. {
  28. /// <summary>
  29. /// Converter for items in the ASP.Net Cache.
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>
  33. /// Outputs an item from the <see cref="HttpRuntime.Cache" />.
  34. /// </para>
  35. /// </remarks>
  36. /// <author>Ron Grabowski</author>
  37. internal sealed class AspNetRequestPatternConverter : AspNetPatternLayoutConverter
  38. {
  39. /// <summary>
  40. /// Write the ASP.Net Cache item to the output
  41. /// </summary>
  42. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  43. /// <param name="loggingEvent">The <see cref="LoggingEvent" /> on which the pattern converter should be executed.</param>
  44. /// <param name="httpContext">The <see cref="HttpContext" /> under which the ASP.Net request is running.</param>
  45. /// <remarks>
  46. /// <para>
  47. /// Writes out the value of a named property. The property name
  48. /// should be set in the <see cref="log4net.Util.PatternConverter.Option"/>
  49. /// property.
  50. /// </para>
  51. /// </remarks>
  52. protected override void Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext)
  53. {
  54. HttpRequest request = null;
  55. try {
  56. request = httpContext.Request;
  57. } catch (HttpException) {
  58. // likely a case of running in IIS integrated mode
  59. // when inside an Application_Start event.
  60. // treat it like a case of the Request
  61. // property returning null
  62. }
  63. if (request != null)
  64. {
  65. if (Option != null)
  66. {
  67. WriteObject(writer, loggingEvent.Repository, httpContext.Request.Params[Option]);
  68. }
  69. else
  70. {
  71. WriteObject(writer, loggingEvent.Repository, httpContext.Request.Params);
  72. }
  73. }
  74. else
  75. {
  76. writer.Write(SystemInfo.NotAvailableText);
  77. }
  78. }
  79. }
  80. }
  81. #endif