src/DotNetCommon.Core/Logger/InternalLogger.cs

(开头部分) 10KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using DotNetCommon.Extensions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetCommon.Logger
{
    internal class LoggerHelper
    {
        internal static bool hasInitQueue = false;
        /// <summary>
        /// 单例
        /// </summary>
        public static readonly ILogger Instance = new InternalLogger<object>("");

        internal static BlockingCollection<LogContext> messageQueue = new BlockingCollection<LogContext>();
    }
    internal class InternalLogger<T> : ILogger, ILogger<T>
    {
        internal InternalLogger(string categoryName)
        {
            this.CategoryName = categoryName;
            InitQueue();
        }

        /// <summary>
        /// 日志类别
        /// </summary>
        public string CategoryName { get; set; }
        private static void InitQueue()
        {
            if (!LoggerHelper.hasInitQueue)
            {
                lock (typeof(ILogger))
                {
                    if (!LoggerHelper.hasInitQueue)
                    {
                        LoggerHelper.hasInitQueue = true;
                        Task.Factory.StartNew(() =>
                        {
                            while (true)
                            {
                                try
                                {
                                    var ctx = LoggerHelper.messageQueue.Take();
                                    LogFile(ctx);
                                }
                                catch (Exception ex)
                                {
                                    LogInternal(ex);
                                }
                            }
                        }, TaskCreationOptions.LongRunning);
                    }
                }
            }
后面还有 192 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。