作者:书友76132462 | 来源:互联网 | 2022-12-02 18:22
我ExceptionLogger
用来处理所有全局异常。我的继承类要求注入依赖项以进行Nlog
调用。
public class NLogExceptionLogger : ExceptionLogger
{
private readonly ILoggingService _loggingService;
public NLogExceptionLogger(ILoggingService loggingService)
{
_loggingService = loggingService;
}
public override void Log(ExceptionLoggerContext context)
{
_loggingService.FirstLevelServiceLog(context.Exception.StackTrace);
}
}
LoggingService类别:
public class LoggingService : ILoggingService
{
private readonly ILogger _logger;
public LoggingService()
{
string currentClassName = typeof(T).Name;
_logger = LogManager.GetLogger(currentClassName);
}
public void FirstLevelServiceLog(string log)
{
_logger.Log(LogLevel.Debug, log);
}
}
我的Unity代码:
public static UnityContainer RegisterComponents()
{
var cOntainer= new UnityContainer();
container.RegisterType(typeof(ILoggingService<>), typeof(LoggingService<>))
}
我正在ExceptionLogger
通过以下方式进行全局注册:(在这一行上,我收到一个错误)
config.Services.Add(typeof(IExceptionLogger), typeof(NLogExceptionLogger));
//Register Dependency Container
config.DependencyResolver = new UnityDependencyResolver(UnityConfig.RegisterComponents());
我在运行时遇到以下错误:
System.ArgumentException:“类型RuntimeType必须从IExceptionLogger派生。”
我的假设是我没有正确注册的依赖项NLogExceptionLogger
。
关于如何在注册服务时解决依赖性的任何想法?
1> Nkosi..:
向ServicesContainer
您添加服务时,请在服务实例中添加类型。
假设已经安装了依赖关系解析程序,如果实例具有依赖关系,则可以用来解析实例。
var logger = config.DependencyResolver.GetService(typeof(NLogExceptionLogger));
config.Services.Add(typeof(IExceptionLogger), logger);
异常记录器和异常处理程序之间也有区别。
我建议您查看以下参考链接,以确定哪个链接适合您的需求。
ASP.NET Web API 2中的参考全局错误处理