Files
COMCMS/COMCMS.Web/Common/HttpGlobalExceptionFilter.cs
T
2018-04-15 23:30:15 +08:00

64 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NewLife.Log;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace COMCMS.Web.Common
{
public class HttpGlobalExceptionFilter : IExceptionFilter
{
readonly IHostingEnvironment _env;
public HttpGlobalExceptionFilter(IHostingEnvironment env)
{
_env = env;
}
public void OnException(ExceptionContext context)
{
//ILog log = LogManager.GetLogger(Startup.repository.Name, typeof(ExceptionContext));
XTrace.WriteException(context.Exception);
//log.Error(context.Exception.Message, context.Exception);
//var logger = _loggerFactory.CreateLogger(context.Exception.TargetSite.ReflectedType);
// logger.LogError(new EventId(context.Exception.HResult),
// context.Exception,
// context.Exception.Message);
//var json = new ErrorResponse("未知错误,请重试");
//if (_env.IsDevelopment()) json.DeveloperMessage = context.Exception;
//context.Result = new ApplicationErrorResult(json);
//context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
//context.ExceptionHandled = true;
}
public class ApplicationErrorResult : ObjectResult
{
public ApplicationErrorResult(object value) : base(value)
{
StatusCode = (int)HttpStatusCode.InternalServerError;
}
}
public class ErrorResponse
{
public ErrorResponse(string msg)
{
Message = msg;
}
public string Message { get; set; }
public object DeveloperMessage { get; set; }
}
}
}