try
{
//Do some stuff
}
catch (NotFoundException e) when (e.Message == "The book {BookId} was not found"){
//Do some more stuff
}
catch (NotFoundException e) when (Resources.BookNotFoundTemplate.Equals(e.Message)){
//Do some more stuff
}
catch (NotFoundException e) when (e.Args[0] is 1234){
//Do some more stuff
}
同样,对于单元测试,当测试异常消息时...
await testedMethod.Should().ThrowAsync<NotFoundException>().Where(e => e.Message == "The book {BookId} was not found");
await testedMethod.Should().ThrowAsync<NotFoundException>().Where(e => e.Args[0] is 1234);
1条答案
按热度按时间de90aj5v1#
我的尝试沿着都是正确的。要使用消息模板记录错误,可以传递异常、模板和参数。
例外情况:
中间件/ExceptionHandler:
仅供参考,使用这种方法的另一个优点(除了过滤日志之外)是,当异常消息中有动态数据时,您还可以更容易地过滤异常。
同样,对于单元测试,当测试异常消息时...