_Core/Utilities/LamdaLogger.cs

(开头部分) 4KB

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

using System;
using System.Collections.Generic;
using System.Linq;

namespace ComLib
{
    /// <summary>
    /// Logger using lamda method to call external logging code.
    /// This decouples code from using a specific ILogging interface via lamdas.
    /// </summary>
    public class LamdaLogger
    {
        private static Action<object, Exception, object[]> _criticalLogger;
        private static Action<object, Exception, object[]> _errorLogger;
        private static Action<object, Exception, object[]> _infoLogger;
        private static Action<object, Exception, object[]> _debugLogger;


        /// <summary>
        /// Initialize default loggers to console.
        /// </summary>
        public LamdaLogger()
        {
            // Default logger to console.
            _criticalLogger = (message, ex, args) => Console.WriteLine(BuildMessage("critical", message, ex, args));
            _errorLogger = (message, ex, args) => Console.WriteLine(BuildMessage("error", message, ex, args));
            _infoLogger = (message, ex, args) => Console.WriteLine(BuildMessage("info", message, ex, args));
            _debugLogger = (message, ex, args) => Console.WriteLine(BuildMessage("debug", message, ex, args));
        }


        /// <summary>
        /// Initialize the different level lamda loggers.
        /// </summary>
        /// <param name="criticalLogger">Critical logger.</param>
        /// <param name="errorLogger">Error logger.</param>
        /// <param name="infoLogger">Information logger.</param>
        /// <param name="debugLogger">Debug logger.</param>
        public void Init(Action<object, Exception, object[]> criticalLogger, Action<object, Exception, object[]> errorLogger, 
            Action<object, Exception, object[]> infoLogger, Action<object, Exception, object[]> debugLogger)
        {
            if (criticalLogger != null) _criticalLogger = criticalLogger;
            if (errorLogger != null) _errorLogger = errorLogger;
            if (infoLogger != null) _infoLogger = infoLogger;
            if (debugLogger != null) _debugLogger = debugLogger;
        }


        /// <summary>
        /// Log a critical error.
        /// </summary>
        /// <param name="message">Error message.</param>
        /// <param name="ex">Relevant exception.</param>
        /// <param name="args">Logging arguments.</param>
        public void Critical(object message, Exception ex = null, object[] args = null)
        {
            if (_criticalLogger != null)
                _criticalLogger(message, ex, args);
        }

后面还有 49 行代码,解锁后查看完整代码

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

AI 解读

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

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