FreeAndNil commented on code in PR #365:
URL: https://github.com/apache/logging-log4net/pull/365#discussion_r4641555406


##########
src/log4net/Core/LoggingEvent.cs:
##########
@@ -1222,19 +1238,23 @@ protected virtual void FixVolatileData(FixFlags flags)
     _cacheUpdatable = false;
   }
 
-  private void CreateCompositeProperties()
+  /// <summary>
+  /// Composes the event's, the calling thread's (when <paramref 
name="withThreadContexts"/>) and the global properties.
+  /// </summary>
+  private CompositeProperties CreateCompositeProperties(bool 
withThreadContexts)
   {
     CompositeProperties compositeProperties = new();
 
     if (_eventProperties is not null)
     {
       compositeProperties.Add(_eventProperties);
     }
-    if (LogicalThreadContext.Properties.GetProperties(false) is 
PropertiesDictionary logicalThreadProperties)
+    if (withThreadContexts

Review Comment:
   Done, also renamed the flag to `includeThreadContexts`.



##########
src/log4net/Layout/Pattern/AspNetPatternLayoutConverter.cs:
##########
@@ -26,25 +26,40 @@
 namespace log4net.Layout.Pattern;
 
 /// <summary>
-/// Abstract class that provides access to the current HttpContext (<see 
cref="HttpContext.Current" />) that 
-/// derived classes need.
+/// Base of the <c>%aspnet-*</c> converters; writes <see 
cref="SystemInfo.NotAvailableText" /> without a current request.
 /// </summary>
 /// <remarks>
-/// This class handles the case when HttpContext.Current is null by writing
-/// <see cref="SystemInfo.NotAvailableText" /> to the writer.
+/// A request scoped converter also writes it for an event fixed with 
properties in another request.
 /// </remarks>
 /// <author>Ron Grabowski</author>
 internal abstract class AspNetPatternLayoutConverter : PatternLayoutConverter
 {
+  private readonly bool _isRequestScoped;
+
+  /// <summary>
+  /// Registers the request capture for a converter that shows request data.
+  /// </summary>
+  /// <param name="isRequestScoped">Whether the converter shows data of the 
current request, not of the application.</param>
+  protected AspNetPatternLayoutConverter(bool isRequestScoped)
+  {
+    _isRequestScoped = isRequestScoped;
+    if (isRequestScoped)
+    {
+      AspNetRequestCapture.Register();
+    }
+  }
+
   protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
   {
-    if (HttpContext.Current is null)
+    // Another request's data would be wrong, not missing.

Review Comment:
   Renamed to `httpContext`, and inverted the condition.



##########
src/log4net/Layout/Pattern/AspNetRequestCapture.cs:
##########
@@ -0,0 +1,91 @@
+#region Apache License
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to you under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+#endregion
+
+#if NET462_OR_GREATER
+
+using System.Runtime.CompilerServices;
+using System.Web;
+using log4net.Core;
+
+namespace log4net.Layout.Pattern;
+
+/// <summary>
+/// A global property that a fix with <see cref="FixFlags.Properties"/> turns 
into a token for the logging request.
+/// </summary>
+/// <remarks>
+/// The token does not reference the request and the table holds it weakly, so 
no ended request is kept alive.
+/// </remarks>
+internal sealed class AspNetRequestCapture : IFixingRequired
+{
+  /// <summary>
+  /// The global property holding the capture, and in a fixed event the token.
+  /// </summary>
+  internal const string Key = "log4net:AspNetRequest";
+
+  private static readonly AspNetRequestCapture _instance = new();
+
+  private static readonly ConditionalWeakTable<HttpContext, RequestToken> 
_tokens = new();
+
+  private AspNetRequestCapture()
+  { }
+
+  /// <summary>
+  /// Registers the capture unless it is registered already, so that a 
registration the application
+  /// cleared comes back with the next request converter.
+  /// </summary>
+  internal static void Register()
+  {
+    // Each set copies the global dictionary, so set only when needed.
+    if (!ReferenceEquals(GlobalContext.Properties[Key], _instance))
+    {
+      GlobalContext.Properties[Key] = _instance;
+    }
+  }
+
+  /// <summary>
+  /// Whether <paramref name="loggingEvent"/> was logged in <paramref 
name="request"/>; an event not
+  /// fixed with properties counts as logged there, being rendered in the call 
that logged it.
+  /// </summary>
+  internal static bool WasLoggedIn(LoggingEvent loggingEvent, HttpContext 
request)
+    => (loggingEvent.Fix & FixFlags.Properties) == 0
+      || (loggingEvent.LookupProperty(Key) is RequestToken token
+        && _tokens.TryGetValue(request, out RequestToken? current) && 
ReferenceEquals(token, current));
+
+  /// <inheritdoc/>
+  object? IFixingRequired.GetFixedObject()
+    => HttpContext.Current is HttpContext request ? _tokens.GetValue(request, 
_ => new RequestToken()) : null;

Review Comment:
   Done.



##########
src/log4net/Util/CompositeProperties.cs:
##########
@@ -116,20 +117,21 @@ public void Add(ReadOnlyPropertiesDictionary properties)
   /// </remarks>
   public PropertiesDictionary Flatten()
   {
-    if (_flattened is null)
+    if (_flattened is PropertiesDictionary flattened)

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to