2017-09-21 5 views
0

다음은 구현 한 것입니다. 을 작성했습니다. ExceptionFilterAttribute에서 상속받은 CustomExceptionFilterAttribute이 작성되었습니다. 각 오류는 if 및 else에 배치되어 적절한 결과를 생성합니다. 내가 무엇을하고 싶습니다 콜백 함수를 만들 수 있도록 if 및 else 블록 및 오류를보다 일반적인 방법으로 처리 할 수 ​​있습니다.asp.net 코어 2.0에서 일반적인 ExceptionFitler 작성

public class HostedServicesController : BaseController 
{ 
    public IActioResult Index() 
    { 
     throw new NotFoundInDatabaseException("Error in Index Controller"); 
    } 
} 

public class NotFoundInDatabaseException : Exception 
{ 
    public NotFoundInDatabaseException(string objectName, object objectId) : 
      base(message: $"No {objectName} with id '{objectId}' was found") 
     { 

     } 
} 

public class CustomExceptionFilterAttribute :ExceptionFilterAttribute 
    { 
     private SystemManager SysMgr { get; } 

     public CustomExceptionFilterAttribute(SystemManager systemManager) 
     { 
      SysMgr = systemManager; 
     } 
     public override void OnException(ExceptionContext context) 
     { 
      var le = SysMgr.Logger.NewEntry(); 
      try 
      { 
       le.Message = context.Exception.Message; 
       le.AddException(context.Exception); 

       var exception = context.Exception; 
       if (exception is NotFoundInDatabaseException) 
       { 
        le.Type = LogType.ClientFaultMinor; 
        context.Result = new NotFoundObjectResult(new Error(ExceptionCode.ResourceNotFound, exception.Message)); 
       } 
       else if (exception is ConfigurationException) 
       { 
        le.Type = LogType.ErrorMinor; 
        context.Result = new BadRequestObjectResult(new Error(ExceptionCode.NotAuthorised, exception.Message)); 
       } 
       else 
       { 
        le.Type = LogType.ErrorSevere; 
        context.Result = new InternalServerErrorObjectResult(new Error(ExceptionCode.Unknown, exception.Message)); 
       } 
       le.AddProperty("context.Result", context.Result); 
       //base.OnException(context); 
      } 
      finally 
      { 
       Task.Run(() => SysMgr.Logger.LogAsync(le)).Wait(); 
      } 
     } 
    } 

답변

0

사용자 정의 유형이 Dictionary 일 수 있습니다. 이런 식의 생각 :

public class ErrorHandlerData 
{ 
    public LogType LogType { get; set; } 
    public string ExceptionCode { get; set; } // not sure if string 
} 

public class CustomExceptionFilterAttribute :ExceptionFilterAttribute 
{ 
    private static Dictionary<Type, ErrorHandlerData> MyDictionary = new Dictionary<Type, ErrorHandlerData>(); 

    static CustomExceptionFilterAttribute() 
    { 
     MyDictionary.Add(typeof(NotFoundInDatabaseException), new ErrorHandlerData 
      { 
       LogType = LogType.ClientFaultMinor, 
       ExceptionCode = ExceptionCode.ResourceNotFound 
      }; 

      //general catch-all 
     MyDictionary.Add(typeof(Exception), new ErrorHandlerData 
      { 
       LogType = LogType.ErrorSevere, 
       ExceptionCode = ExceptionCode.Unknown 
      }; 
    } 

그래서 당신은 다음과 같이 사용할 수 있습니다 :

public override void OnException(ExceptionContext context) 
{ 
    var le = SysMgr.Logger.NewEntry(); 
    try 
    { 
     le.Message = context.Exception.Message; 
     le.AddException(context.Exception); 

     var exception = context.Exception; 
     var exeptionType = exception.GetType(); 
     var errorHandlerData = MyDictionary.ContainsKey(exceptionType) ? 
      MyDictionary[exceptionType] : MyDictionary[typeof(Exception)]; 

     le.Type = errorHandlerData.LogType; 
     context.Result = new NotFoundObjectResult(new Error(errorHandlerData.ExceptionCode, exception.Message)); 

     le.AddProperty("context.Result", context.Result); 
    } 
    finally 
    { 
     Task.Run(() => SysMgr.Logger.LogAsync(le)).Wait(); 
    } 
} 
관련 문제