using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Nirvana.Common
{
    public class LogFactory
    {
        public static Logger _logger;
        static LogFactory()
        {

        }

        public static Logger GetLogger(IConfiguration configuration)
        {
            _logger = new LoggerConfiguration()
             .ReadFrom.Configuration(configuration)
                //.MinimumLevel.Information()
                //.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                //.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
                .Enrich.FromLogContext()
                .WriteTo.Map(
                    evt => evt.Level,
                    (level, wt) => wt.File($"{Configs.GetString("logfile")}/{DateTime.Now.ToString("yyyy-MM-dd")}/" + level + ".log", outputTemplate: "{Timestamp:HH:mm:ss} ({Application}/{MachineName}/{ThreadId})({SourceContext}){NewLine}{Message}{NewLine}{Exception}============================{NewLine}")
                )
                .CreateLogger();
            return _logger;
        }

        public static void InsertErrorLog(string msg, int type = 1)
        {
            msg = $"{msg}";
            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;
            }
        }
    }
}
