public class MyExceptionFilter : IExceptionFilter
{
private ILogService logService;
public MyExceptionFilter(ILogService logService)
{
this.logService = logService;
}
public void OnException(ExceptionContext context)
{
logService.AddLog(context.Exception.Message);
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(typeof(MyExceptionFilter));
// services.AddMvc();
services.AddMvc(options =>
{
var serviceProvider = services.BuildServiceProvider();
var filter= serviceProvider.GetService<MyExceptionFilter>();
options.Filters.Add(filter);
});
Assembly asm = Assembly.Load(new AssemblyName("IOC.Services"));
var serivesTypes = asm.GetTypes().Where(t => typeof(IServiceSupport).IsAssignableFrom(t)
&& !t.GetTypeInfo().IsAbstract);
foreach (var sericeType in serivesTypes)
{
foreach (var intfType in sericeType.GetInterfaces())
{
services.AddSingleton(intfType, sericeType);
}
}
}