using CallCenter.Utility;
using System.Web.Mvc;

namespace CallCenterApi.Interface
{
    public class ErrorAttribute : HandleErrorAttribute
    {
        //此处代码说明，将所有网站异常的日志全部记录 log4net error文件
        public override void OnException(ExceptionContext context)
        {
            base.OnException(context);
            WriteLog(context);
            context.ExceptionHandled = true;
            context.HttpContext.Response.StatusCode = 200;
            context.Result = new ContentResult { Content = new AjaxResult { state = ResultTypes.error.ToString(), message = context.Exception.ToString()}.ToJson() };
        }

        private void WriteLog(ExceptionContext context)
        {
            if (context == null)
                return;
            var actionName = context.RouteData.Values["Action"];
            var log = LogFactory.GetLogger(context.Controller.ToString()+"/"+actionName);
            log.Error(context.ToString());
        }
    }
}