using GuideScreen.Common.Common;
using PVDEMCS.Services;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PVDEMCS.Common.Log
{
    /// <summary>
    /// 日志扩展
    /// </summary>
    public static class LogExtensions
    {
        private static ILogService _logService;

        static LogExtensions()
        {

        }

        /// <summary>
        /// 日志服务
        /// </summary>
        public static  void Load(ILogService logService)
        {
            _logService = logService;
        }
        /// <summary>
        /// 写入Error级别日志 
        /// </summary>
        /// <param name="ex">异常对象</param>
        public static void WriteErrorLog(this Exception ex,string title="")
        {
            var error = new StringBuilder();
            error.AppendLine(title);
            while (ex != null)
            {
                error.AppendLine($"{ex.Message}");
                error.AppendLine($"{ex.StackTrace};");
                ex = ex.InnerException;
            }
            _logService.Error(error.ToString());
        }

        /// <summary>
        /// 写入Fatal级别日志 
        /// </summary>
        /// <param name="ex">异常对象</param>
        public static void WriteFatalLog(this Exception ex, string title = "")
        {
            var error = new StringBuilder();
            error.AppendLine(title);
            while (ex != null)
            {
                error.AppendLine($"{ex.Message}");
                error.AppendLine($"{ex.StackTrace};");
                ex = ex.InnerException;
            }
            _logService.Fatal(error.ToString());
        }

        /// <summary>
        /// 写入info级别日志
        /// </summary>
        /// <param name="str"></param>
        public static void WriteInfoLog(this string str)
        {
            _logService.Info(str);
        }

        /// <summary>
        /// 写入debug级别日志
        /// </summary>
        /// <param name="str"></param>
        public static void WriteDebugLog(this string str)
        {
            _logService.Debug(str);
        }

        /// <summary>
        /// 写入Error级别日志
        /// </summary>
        /// <param name="str"></param>
        public static void WriteErrorLog(this string str)
        {
            _logService.Error(str);
        }

        /// <summary>
        /// 写入Error级别日志
        /// </summary>
        /// <param name="str"></param>
        public static void WriteFatalLog(this string str)
        {
            _logService.Fatal(str);
        }
    }
}
