using Furion.DependencyInjection;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Waste.Application
{
    /// <summary>
    /// 日志处理
    /// </summary>
    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"></param>
        public void AddErrorLogger(Exception ex, string param = "", string program = "", int type = 1)
        {
            var msg = $"ex:{ex.Message}\r\nsource:{ex.Source}\r\nstack:{ex.StackTrace}\r\n";
            if (ex.InnerException != null)
            {
                msg = $"{msg}InnerException.Message:{ex.InnerException.Message}\r\nInnerException.Source:{ex.InnerException.Source}\r\nInnerException.StackTrace:{ex.InnerException.StackTrace}\r\n";
            }
            if (!string.IsNullOrEmpty(param))
            {
                msg = $"参数:{param}\r\n{msg}";
            }
            if (!string.IsNullOrEmpty(program))
            {
                msg = $"接口:{program}\r\n{msg}";
            }
            AddLogger(msg, type);
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="type"></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;
            }
        }
    }
}
