【发布时间】:2018-10-04 20:23:50
【问题描述】:
我在我的 Web API 项目中使用 Nlog 作为日志框架,并带有 Unity 一个 IoC 容器。
我有LoggingService 类,它将类名作为参数并返回一个 NLog 实例。为此,我使用了依赖注入。
问题:我很困惑如何将类名传递给我的LoggingService 类?
代码:
using NLog;
namespace Portal.Util.Logging
{
public class LoggingService : ILoggingService
{
private readonly ILogger _logger;
public LoggingService(string currentClassName)
{
_logger = LogManager.GetLogger(currentClassName);
}
public void FirstLevelServiceLog(string log)
{
_logger.Log(LogLevel.Debug, log);
}
}
public interface ILoggingService
{
void FirstLevelServiceLog(string log);
}
}
服务层:(这是从控制器调用的)
public class MyService : IMyService
{
private readonly ILoggingService _loggingService;
public MyService(ILoggingService loggingService)
{
_loggingService = loggingService
}
public DoSomething()
{
_loggingService.FirstLevelServiceLog("Debug");
}
}
团结:
var container = new UnityContainer();
container.RegisterType<ILoggingService, LoggingService>(new InjectionConstructor(""))
/* Not sure on how to pass the class name here? */
【问题讨论】:
标签: asp.net-web-api dependency-injection unity-container nlog