如何使用Autofac注入NLog



我正在使用Autofac和NLog编写一个WinForm应用程序。所以我创造了一个界面:名称空间BookShopApp。日志

{
public interface ILoggerService<T> where T : class
{
void Info(string message);
void Warn(string message);
void Error(string message);
void Trace(string message);
}
}

然后实现

public class LoggerService<T> : ILoggerService<T> where T : class
{
public Logger _logger { get; set; }
public LoggerService(ILogger logger)
{
_logger = LogManager.GetLogger(typeof(T).FullName);
}
public void Error(string message)
{
_logger.Error(message);
}
public void Info(string message)
{
_logger.Info(message);
}
public void Trace(string message)
{
_logger.Trace(message);
}
public void Warn(string message)
{
_logger.Warn(message);
}
}

之后我在模块

中注册了这个
internal class ServiceModule:Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<GetBookService>().As<IGetBookService>();
builder.RegisterType<AddPublisherService>().As<IAddPublisherService>();
builder.RegisterType<AddAuthorService>().As<IAddAuthorService>();
builder.RegisterType<AddBookService>().As<IAddBookService>();
builder.RegisterType<GetPublisherService>().As<IGetPublishersService>();
builder.RegisterType<GetAuthorsService>().As<IGetAuthorsService>();
builder.RegisterType<SaleBookService>().As<ISaleBookService>();
builder.RegisterType<ChangePriceService>().As<IChangePriceService>();
builder.RegisterType<GetPublisherByNameService>().As<IGetPublisherByNameService>();
builder.RegisterType<CreateCartService>().As<ICreateCartService>();
builder.RegisterType<GetLastCartDetails>().As<IGetLastCartDetails>();
builder.RegisterType<RemoveUnsoldCartsService>().As<IRemoveUnsoldCartsService>();
builder.RegisterType<CreateBookListToChangeService>().As<ICreateBookListToChangeService>();
builder.RegisterType<GetBooksToChangeService>().As<IGetBooksToChangeService>();
builder.RegisterType<RemoveUnchangedBooksService>().As<IRemoveUnchangedBooksService>();
builder.RegisterType<Logger>().As<ILogger>();
builder.RegisterGeneric(typeof(LoggerService<>)).As(typeof(ILoggerService<>)).InstancePerLifetimeScope();
builder.RegisterType<Logger>().As<ILogger>();
builder.RegisterType<DataContext>().AsSelf();
builder.RegisterType<PurchaseBooksReport>().AsSelf();
builder.RegisterType<BooksOfAuthorReport>().AsSelf();
builder.RegisterType<AddPublisherForm>().AsSelf();
builder.RegisterType<AddAuthorForm>().AsSelf();
builder.RegisterType<AddBookForm>().AsSelf();
builder.RegisterType<ChangePriceForm>().AsSelf();
builder.RegisterType<CreateSaleForm>().AsSelf();
builder.RegisterType<BookShopForm>();
builder.RegisterType<ReportsForm>().AsSelf();
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
.AssignableTo<DevExpress.XtraEditors.XtraForm>()
.AsSelf();
}
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
var builder = new ContainerBuilder();
builder.RegisterModule<ServiceModule>();
using var container=builder.Build();
var form=container.Resolve<BookShopForm>();
Application.Run(form);
}

运行应用程序时,我得到了异常未找到类型"NLog.Logger"的可访问构造函数。"我不明白我做错了什么。这是因为Logger没有公共actor。那么如何注射呢?

我当前的实现

using NLog;
namespace Core.Aspects.Log
{
public class Logger : ILog
{
private readonly ILogger _logger =  LogManager.GetCurrentClassLogger();
public void Error(string message)
{
_logger.Error(message);
}
public void Fatal(string message)
{
_logger.Fatal(message);
}
public void Info(string message)
{
_logger.Info(message);  
}
public void Warn(string message)
{
_logger.Warn(message);
}
}
}
services.AddSingleton<ILog, Logger>();

public interface ILog
{
void Info(string message);
void Error(string message);
void Fatal(string message);
void Warn(string message);
}

可能在AutoFac中注册Microsoft Extensions LoggingLoggerFactory+ILogger:

var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<LoggerFactory>()
.As<ILoggerFactory>()
.SingleInstance();
containerBuilder.RegisterGeneric(typeof(Logger<>))
.As(typeof(ILogger<>))
.SingleInstance();

然后使用NLog. extensions . logging:

将NLog注册为日志提供商
IServiceProvider serviceProvider = new AutofacServiceProvider(container);
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
loggerFactory.AddNLog();

那么你可以获得一个类型的ILogger<T>:

serviceProvider.GetRequiredService<ILogger<ServiceModule>>().LogDebug("Logging initialized!");

参见:https://github.com/NLog/NLog.Extensions.Logging/issues/225

相关内容

  • 没有找到相关文章

最新更新