Repository: microsoft/PowerToys
File: src/common/ManagedCommon/Logger.cs (C#)

Code before:

            var logFilePath = Path.Combine(versionedPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".log");

            Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));

            Trace.AutoFlush = true;

            // Clean up old version log folders
            Task.Run(() => DeleteOldVersionLogFolders(basePath, versionedPath));
        }

        /// <summary>
        /// Deletes old version log folders, keeping only the current version's folder.
        /// </summary>
        /// <param name="basePath">The base path to the log files folder.</param>
        /// <param name="currentVersionPath">The path to the current version's log folder.</param>
        private static void DeleteOldVersionLogFolders(string basePath, string currentVersionPath)
        {
            try
            {
                if (!Directory.Exists(basePath))
                {
                    return;
                }

                foreach (string directory in Directory.GetDirectories(basePath))
                {
                    if (!directory.Equals(currentVersionPath, StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            Directory.Delete(directory, true);
                            LogInfo($"Deleted old log directory: {directory}");
                        }
                        catch (Exception ex)
                        {
                            LogError($"Failed to delete old log directory: {directory}", ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogError("Error cleaning up old log folders", ex);
            }
        }

        public static void LogError(string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(message, Error, memberName, sourceFilePath, sourceLineNumber);
        }

Review comment (performance):
Can we just do the olderversion? e.g.
// delete in order 
var dirs = Directory.GetDirectories(basePath)
    .Select(d => new DirectoryInfo(d))
    .OrderBy(d => d.CreationTime)
    .ToList();

// Exclude the last 2 directories, and only take 3 for performance consideration
var dirsToIterate = dirs.Take(Math.Max(0, dirs.Count - 2)).Take(3);

Code after:
            }

            var logFilePath = Path.Combine(versionedPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".log");

            Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));

            Trace.AutoFlush = true;

            // Clean up old version log folders
            Task.Run(() => DeleteOldVersionLogFolders(basePath, versionedPath));
        }

        /// <summary>
        /// Deletes old version log folders, keeping only the current version's folder.
        /// </summary>
        /// <param name="basePath">The base path to the log files folder.</param>
        /// <param name="currentVersionPath">The path to the current version's log folder.</param>
        private static void DeleteOldVersionLogFolders(string basePath, string currentVersionPath)
        {
            try
            {
                if (!Directory.Exists(basePath))
                {
                    return;
                }

                var dirs = Directory.GetDirectories(basePath)
                    .Select(d => new DirectoryInfo(d))
                    .OrderBy(d => d.CreationTime)
                    .Where(d => !string.Equals(d.FullName, currentVersionPath, StringComparison.OrdinalIgnoreCase))
                    .Take(3)
                    .ToList();

                foreach (var directory in dirs)
                {
                    try
                    {
                        Directory.Delete(directory.FullName, true);
                        LogInfo($"Deleted old log directory: {directory.FullName}");
                        Task.Delay(500).Wait();
                    }
                    catch (Exception ex)
                    {
                        LogError($"Failed to delete old log directory: {directory.FullName}", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                LogError("Error cleaning up old log folders", ex);
            }