net core exception handling in asp.net core 5 project but I want to保存the exception in sql server database,but unfortunately when I inject that class have the insert method of exception to database it throw null reference exception for injected interface into startup class:
private readonly IExceptionService exceptionService;
public Startup(IExceptionService exceptionService)
{
this.exceptionService = exceptionService;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ app.UseExceptionHandler(a => a.Run(async context =>
{
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = exceptionHandlerPathFeature.Error;
// For Save Exception Log
if (exceptionService is not null)
{
exceptionService.AddException(exception);
}
// For Show Json Error in Resoponse
await context.Response.WriteAsJsonAsync(new DTOResultObject(exception.Message));
}));
}
我的启动类在第一个有另一个构造函数:
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
能帮我解决这个问题吗?谢谢,空引用Exception Error正好是这一行的exceptionService:
if (exceptionService is not null)
{
exceptionService.AddException(exception);
}
1条答案
按热度按时间vuktfyat1#
你应该像下面这样在
ConfigureServices
方法中注册它。我也做了一些研究,找到一个有用的回购给你。它像下面这样注册
ExceptionService
您可以参考此内容来实现Your_SQLExceptionService,如下所示