using Furion.DependencyInjection;
using Serilog;
using System;

namespace YBDevice.Core
{
    public class LoggerService : ILoggerService, ITransient
    {
        private readonly ILogger _logger;
        public LoggerService(ILogger logger)
        {
            _logger = logger;
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="ex">异常信息</param>
        /// <param name="param">参数</param>
        /// <param name="program">程序</param>
        /// <param name="type">1-error,2-information,3-warning,4-debug,5-fatal</param>
        public void AddErrorLogger(Exception ex, string param = "", string program = "", int type = 1)
        {
            var msg = string.Empty;
            if (!string.IsNullOrEmpty(param))
            {
                msg = $"参数:{param}";
            }
            if (!string.IsNullOrEmpty(program))
            {
                msg = string.IsNullOrEmpty(msg)?$"接口:{program}": $"接口:{program}\r\n{msg}";
            }
            AddLogger(ex,msg, type);
        }
        public void AddLogger(Exception ex,string msg,int type)
        {
            switch (type)
            {
                case 1:
                    _logger.Error(ex,msg);
                    break;
                case 2:
                    _logger.Information(ex, msg);
                    break;
                case 3:
                    _logger.Warning(ex, msg);
                    break;
                case 4:
                    _logger.Debug(ex, msg);
                    break;
                case 5:
                    _logger.Fatal(ex, msg);
                    break;
                default:
                    _logger.Error(ex, msg);
                    break;
            }
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="type">1-error,2-information,3-warning,4-debug,5-fatal</param>
        public void AddLogger(string msg, int type)
        {
 
            switch (type)
            {
                case 1:
                    _logger.Error(msg);
                    break;
                case 2:
                    _logger.Information(msg);
                    break;
                case 3:
                    _logger.Warning(msg);
                    break;
                case 4:
                    _logger.Debug(msg);
                    break;
                case 5:
                    _logger.Fatal(msg);
                    break;
                default:
                    _logger.Error(msg);
                    break;
            }
        }
    }
}
