AspNetCachePatternConverter.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. // .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 AspNetCachePatternConverter : 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. If no property has been set, all key value pairs from the Cache will
  50. /// be written to the output.
  51. /// </para>
  52. /// </remarks>
  53. protected override void Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext)
  54. {
  55. if (HttpRuntime.Cache != null)
  56. {
  57. if (Option != null)
  58. {
  59. WriteObject(writer, loggingEvent.Repository, HttpRuntime.Cache[Option]);
  60. }
  61. else
  62. {
  63. WriteObject(writer, loggingEvent.Repository, HttpRuntime.Cache.GetEnumerator());
  64. }
  65. }
  66. else
  67. {
  68. writer.Write(SystemInfo.NotAvailableText);
  69. }
  70. }
  71. }
  72. }
  73. #endif