winforms 如何使用Autofac注入NLog

yjghlzjz  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(324)

我正在用Autofac和NLog编写一个WinForm应用程序,所以我创建了一个界面:命名空间BookShopApp.Logging

{
    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);
        }

运行应用程序时,我得到了异常“No accessible constructors were found for the type 'NLog. Logger'.”我不明白我做错了什么。这是因为Logger没有公共ctors。那么如何注入它呢?

f2uvfpb9

f2uvfpb91#

我当前的实现

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);
    }

相关问题