AspNetPatternConverter.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /// Abstract class that provides access to the current HttpContext (<see cref="HttpContext.Current" />) that
  30. /// derived classes need.
  31. /// </summary>
  32. /// <remarks>
  33. /// This class handles the case when HttpContext.Current is null by writing
  34. /// <see cref="SystemInfo.NotAvailableText" /> to the writer.
  35. /// </remarks>
  36. /// <author>Ron Grabowski</author>
  37. internal abstract class AspNetPatternLayoutConverter : PatternLayoutConverter
  38. {
  39. protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
  40. {
  41. if (HttpContext.Current == null)
  42. {
  43. writer.Write(SystemInfo.NotAvailableText);
  44. }
  45. else
  46. {
  47. Convert(writer, loggingEvent, HttpContext.Current);
  48. }
  49. }
  50. /// <summary>
  51. /// Derived pattern converters must override this method in order to
  52. /// convert conversion specifiers in the correct way.
  53. /// </summary>
  54. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  55. /// <param name="loggingEvent">The <see cref="LoggingEvent" /> on which the pattern converter should be executed.</param>
  56. /// <param name="httpContext">The <see cref="HttpContext" /> under which the ASP.Net request is running.</param>
  57. protected abstract void Convert(TextWriter writer, LoggingEvent loggingEvent, HttpContext httpContext);
  58. }
  59. }
  60. #endif