<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using static System.DateTime;

namespace Masuit.Tools.Logging
{
    /// <summary>
    /// 日志组件
    /// </summary>
    public class LogManager
    {
        static readonly ConcurrentQueue<Tuple<string, string>> LogQueue = new ConcurrentQueue<Tuple<string, string>>();

        /// <summary>
        /// 自定义事件
        /// </summary>
        public static event Action<LogInfo> Event;

        static LogManager()
        {
            var writeTask = new Task(obj =>
            {
                while (true)
                {
                    Pause.WaitOne(1000, true);
                    List<string[]> temp = new List<string[]>();
                    foreach (var logItem in LogQueue)
                    {
                        string logPath = logItem.Item1;
                        string logMergeContent = string.Concat(logItem.Item2, Environment.NewLine, "----------------------------------------------------------------------------------------------------------------------", Environment.NewLine);
                        string[] logArr = temp.FirstOrDefault(d => d[0].Equals(logPath));
                        if (logArr != null)
                        {
                            logArr[1] = string.Concat(logArr[1], logMergeContent);
                        }
                        else
                        {
                            logArr = new[]
                            {
                                logPath,
                                logMergeContent
                            };
                            temp.Add(logArr);
                        }

                        LogQueue.TryDequeue(out Tuple<string, string> _);
                    }

                    foreach (var item in temp)
                    {
                        WriteText(item[0], item[1]);
                    }
                }
            }, null, TaskCreationOptions.LongRunning);
            writeTask.Start();
        }

        private static AutoResetEvent Pause => new AutoResetEvent(false);

        /// <summary>
        /// 日志存放目录，默认日志放在当前应用程序运行目录下的logs文件夹中
        /// </summary>
        public static string LogDirectory
        {
            get => Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).Any(s => s.Contains("Web.config")) ? AppDomain.CurrentDomain.BaseDirectory + @"App_Data\Logs\" : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
            set
            {
            }
        }

        /// <summary>
        /// 写入Info级别的日志
        /// </summary>
        /// <param name="info"></param>
        public static void Info(string info)
        {
            LogQueue.Enqueue(new Tuple<string, string>(GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(info).ToUpper()}  {info}"));
            var log = new LogInfo()
            {<commit_msg>!4 修复自定义日志目录无效的问题 Merge pull request !4 from Jerry/N/A<commit_after>using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using static System.DateTime;

namespace Masuit.Tools.Logging
{
    /// <summary>
    /// 日志组件
    /// </summary>
    public class LogManager
    {
        static readonly ConcurrentQueue<Tuple<string, string>> LogQueue = new ConcurrentQueue<Tuple<string, string>>();

        /// <summary>
        /// 自定义事件
        /// </summary>
        public static event Action<LogInfo> Event;

        static LogManager()
        {
            var writeTask = new Task(obj =>
            {
                while (true)
                {
                    Pause.WaitOne(1000, true);
                    List<string[]> temp = new List<string[]>();
                    foreach (var logItem in LogQueue)
                    {
                        string logPath = logItem.Item1;
                        string logMergeContent = string.Concat(logItem.Item2, Environment.NewLine, "----------------------------------------------------------------------------------------------------------------------", Environment.NewLine);
                        string[] logArr = temp.FirstOrDefault(d => d[0].Equals(logPath));
                        if (logArr != null)
                        {
                            logArr[1] = string.Concat(logArr[1], logMergeContent);
                        }
                        else
                        {
                            logArr = new[]
                            {
                                logPath,
                                logMergeContent
                            };
                            temp.Add(logArr);
                        }

                        LogQueue.TryDequeue(out Tuple<string, string> _);
                    }

                    foreach (var item in temp)
                    {
                        WriteText(item[0], item[1]);
                    }
                }
            }, null, TaskCreationOptions.LongRunning);
            writeTask.Start();
        }

        private static AutoResetEvent Pause => new AutoResetEvent(false);

        private static string logDirectory;
        /// <summary>
        /// 日志存放目录，默认日志放在当前应用程序运行目录下的logs文件夹中
        /// </summary>
        public static string LogDirectory
        {
            get => logDirectory ?? Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).Any(s => s.Contains("Web.config")) ? AppDomain.CurrentDomain.BaseDirectory + @"App_Data\Logs\" : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
            set
            {
                //自定义目录
                if (!Directory.Exists(value))
                {
                    Directory.CreateDirectory(value);
                }
                logDirectory = value;
            }
        }

        /// <summary>
        /// 写入Info级别的日志
        /// </summary>
        /// <param name="info"></param>
        public static void Info(string info)
        {
            LogQueue.Enqueue(new Tuple<string, string>(GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(info).ToUpper()}  {info}"));
            var log = new LogInfo()
            {
{'fraction_non_alphanumeric': 0.13936203158872715, 'fraction_numerical': 0.0037163208423660575, 'mean_word_length': 1.1813576494427558, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 51, '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': '6952328', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1593, 'n_words': 300}