fluffynuts commented on code in PR #319:
URL: https://github.com/apache/logging-log4net/pull/319#discussion_r4013391474


##########
src/log4net/Appender/FileAppender.cs:
##########
@@ -1358,6 +1368,40 @@ protected virtual void SetQWForFiles(TextWriter writer)
   /// </remarks>
   protected static string ConvertToFullPath(string path) => 
SystemInfo.ConvertToFullPath(path);
 
+  /// <summary>
+  /// Names the mutex that serialises <paramref name="path"/> between 
processes, with
+  /// <paramref name="suffix"/> telling one mutex over the same file from 
another.
+  /// </summary>
+  /// <remarks>
+  /// The flattened path, as earlier versions computed it. Only a name the 
platform rejects is
+  /// hashed: Unix stops at <see cref="MaxMutexNameLength"/>, Windows has no 
limit. Unprefixed, so
+  /// on Windows it coordinates one session.
+  /// </remarks>
+  internal static string MutexNameForPath(string path, string suffix)
+    => MutexNameForPath(path, suffix, SystemInfo.IsWindows ? null : 
MaxMutexNameLength);
+
+  /// <summary>Takes the limit rather than deciding it, so both branches are 
testable anywhere.</summary>
+  private static string MutexNameForPath(string path, string suffix, int? 
maxLength)
+  {
+    string name = path.EnsureNotNull()
+      .Replace("\\", "_")
+      .Replace(":", "_")
+      .Replace("/", "_") + suffix;
+
+    if (maxLength is null || name.Length <= maxLength)
+    {
+      return name;
+    }
+
+    // TODO use SHA256.HashData and Convert.ToHexString on .net10

Review Comment:
   I assume this is still in-progress (:



##########
src/log4net.Tests/Appender/FileAppenderMutexNameTest.cs:
##########
@@ -0,0 +1,222 @@
+#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
+
+using System;
+using System.IO;
+using System.Reflection;
+using System.Text;
+using System.Threading;
+
+using log4net.Appender;
+using log4net.Core;
+using log4net.Layout;
+using log4net.Util;
+
+using NUnit.Framework;
+
+namespace log4net.Tests.Appender;
+
+/// <summary>The mutex name the file lock and the rolling lock derive from the 
log file path.</summary>
+[TestFixture]
+public sealed class FileAppenderMutexNameTest
+{
+  /// <summary>Records what the appender had resolved by the time the locking 
model was activated.</summary>
+  private sealed class RecordingLock : FileAppender.LockingModelBase
+  {
+    internal string? FileAtActivation { get; private set; }
+
+    public override void ActivateOptions() => FileAtActivation = 
CurrentAppender?.File;
+
+    public override Stream? AcquireLock() => Stream.Null;
+
+    public override void ReleaseLock()
+    { }
+
+    public override void OpenFile(string filename, bool append, Encoding 
encoding)
+    { }
+
+    public override void CloseFile()
+    { }
+
+    public override void OnClose()
+    { }
+  }
+
+  private string _directory = string.Empty;
+
+  [SetUp]
+  public void SetUp()
+  {
+    _directory = Path.Combine(Path.GetTempPath(), 
Guid.NewGuid().ToString("N"));

Review Comment:
   very minor: if you'd like to keep stuff in the test, you're welcome to try 
PeanutButter.Utils' AutoTempFolder - a disposable which creates a folder in the 
system temp location and destroys it when disposed, and you can use it like:
   
   ```csharp
   using var dir = new AutoTempFolder();
   var pathToFile = Path.Combine(dir.Path, "some-file");
   ```
   
   I must admit that I have an aversion to storing state on a test fixture and 
keeping that state useful via setup/teardown methods - I've found that at worst 
it introduces shared state that someone forgets is shared, failing a test, and 
at best, it means that when I'm trying to figure out what a test actually does, 
I have to hop around the test fixture. Anyhoo, it's just a preference of mine.



-- 
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