2

I am using ExceptionLogger to handle all the global exception. My inheriting class requires dependencies to be injected for Nlog to invoke.

public class NLogExceptionLogger : ExceptionLogger
{
    private readonly ILoggingService _loggingService;

    public NLogExceptionLogger(ILoggingService<NLogExceptionLogger> loggingService)
    {
        _loggingService = loggingService;
    }

    public override void Log(ExceptionLoggerContext context)
    {
        _loggingService.FirstLevelServiceLog(context.Exception.StackTrace);
    }
}

LoggingService Class:

public class LoggingService<T> : ILoggingService<T>
{
    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);
    }
}

My Unity Code:

public static UnityContainer RegisterComponents()
{
    var container = new UnityContainer();
    container.RegisterType(typeof(ILoggingService<>), typeof(LoggingService<>))
}

I am registering ExceptionLogger globally by doing: (On this line i am getting an error)

config.Services.Add(typeof(IExceptionLogger), typeof(NLogExceptionLogger));
//Register Dependency Container
config.DependencyResolver = new UnityDependencyResolver(UnityConfig.RegisterComponents());

I am getting following error at runtime:

System.ArgumentException: 'The type RuntimeType must derive from IExceptionLogger.'

My assumption is i am not properly registering the dependency for NLogExceptionLogger.

Any idea on how to resolve dependency while registering the service?

3
  • Did you properly set up the dependency resolver? And also When adding service you add the instance. config.Services.Add(typeof(IExceptionLogger), {instance here}); Commented Oct 8, 2018 at 20:51
  • @Nkosi: Yes, I have edited the question with resolver code. Commented Oct 8, 2018 at 21:05
  • Right I would suggest you set that before adding the service Commented Oct 8, 2018 at 21:07

1 Answer 1

5

When adding service to ServicesContainer you add the type with the service instance.

Assuming dependency resolver has already been setup, that can be used to resolve the instance if it has dependencies.

var logger = config.DependencyResolver.GetService(typeof(NLogExceptionLogger));
config.Services.Add(typeof(IExceptionLogger), logger);

There is also a difference between exceptions loggers and exception handlers.

I suggest reviewing the following reference link to determine which one is appropriate for your needs.

Reference Global Error Handling in ASP.NET Web API 2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.