﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Ctrip.SOA.Comm.Log.LogImpl;
using Ctrip.SOA.Comm.Utility;
using Ctrip;
using Ctrip.Core;
using Ctrip.Repository;
using Ctrip.Repository.Hierarchy;
using Ctrip.Appender;

namespace Ctrip.SOA.Comm.Log
{
    public class Logger
    {
        public static readonly string ErrorLoggerName = "ErrorLogger";
        public static readonly string EsbLoggerName = "EsbLogger";
        public static readonly string DefaultLoggerName = "DefaultLogger";

        static Logger()
        {
            Configure();
        }

        public static string ESBLogUrl
        {
            get;
            set;

        }

        public static ResponseLog ResponseLog
        {
            get
            {
                return LogHandleFactory.CreateHandler<ResponseLog>();
            }
        }

        //ESB未使用
        public static AutoSendLog AutoSendLog
        {
            get
            {
                return LogHandleFactory.CreateHandler<AutoSendLog>();
            }
        }

        public static CounterLog CounterLog
        {
            get
            {
                return LogHandleFactory.CreateHandler<CounterLog>();
            }
        }

        public static ESBCheckFailLog ESBCheckFailLog
        {
            get
            {
                return LogHandleFactory.CreateHandler<ESBCheckFailLog>();
            }
        }

        public static ESBLog ESBLog
        {
            get
            {
                return LogHandleFactory.CreateHandler<ESBLog>();
            }
        }

        public static PerformanceTickLog PerformanceTickLog
        {
            get
            {
                return LogHandleFactory.CreateHandler<PerformanceTickLog>();
            }
        }

        public static TraceLog Default
        {
            get
            {
                return LogHandleFactory.CreateHandler<TraceLog>();
            }
        }

        private static void Configure()
        {
            var udpAppender = new UdpAppender
            {
                RemoteAddress = IPAddress.Parse(AppSetting.UdpUdpTraceIp),
                RemotePort = AppSetting.UdpTracePort,
                Layout = new TraceLogPatternLayout(),
                Encoding = Encoding.UTF8
            };

            var cloggingAppender = new CLoggingAppender();
            var esbAppender = new EsbLogAppender();
            udpAppender.ActivateOptions();
            esbAppender.ActivateOptions();
            cloggingAppender.ActivateOptions();

            var repository = LogManager.GetRepository() as Hierarchy;
            repository.Root.Level = Level.Info;
            repository.Root.AddAppender(cloggingAppender);

            var esbLogger = repository
                .GetLogger(EsbLoggerName) as Ctrip.Repository.Hierarchy.Logger;
            esbLogger.Additivity = false;
            esbLogger.Level = Level.Info;
            esbLogger.AddAppender(esbAppender);

            var esbErrorLogger = repository
                .GetLogger(ErrorLoggerName) as Ctrip.Repository.Hierarchy.Logger;
            esbErrorLogger.Level = Level.Error;
            esbErrorLogger.AddAppender(esbAppender);

            repository.Configured = true;
        }
    }
}
