ILoggerFactory和structuremap注册

时间:2018-03-12 15:44:56

标签: c# asp.net .net

在我的API项目中,Startup.cs文件我有以下内容:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();

        app.UseMvc();

        loggerFactory.AddAWSProvider(Configuration.GetAWSLoggingConfigSection(),
            (logLevel, message, exception) => $"[{DateTime.UtcNow}] {logLevel}: {message}");

这适用于API项目。我如何在Structuremap容器中注册它?

var container = new StructureMap.Container(
                c =>
                {
                    c.For(typeof(ILogger<>)).Use(typeof(Logger<>));
                    c.For<ILoggerFactory>..../// use what?

    }

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要注册loggerFactory的现有实例。有Use方法的重载需要现有实例:

var container = new StructureMap.Container(
    c =>
    {
        c.For(typeof(ILogger<>)).Use(typeof(Logger<>));
        c.For(typeof(ILoggerFactory)).Use(loggerFactory);
    });

有关详细信息,请参阅Registering Existing Objects