将Asp.Net Core 2注入用于带有多个项目的Serilog [英] Using Asp.Net Core 2 Injection for Serilog with Multiple Projects

查看:329
本文介绍了将Asp.Net Core 2注入用于带有多个项目的Serilog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Asp.Net Core 2.0配置了Serilog,并且在启动Web项目中通过.Net Core依赖项注入(如果我通过Microsoft.Extensions.Logging使用它)可以很好地工作,但是我无法从任何方式访问它其他项目.

I have Serilog configured for Asp.Net Core 2.0 and it works great via .Net Core dependency injection in my startup web project (if I use it through Microsoft.Extensions.Logging), but I can't access it from any other project.

这就是我所拥有的:

Program.cs

using System;
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;

namespace ObApp.Web.Mvc
{
    public class Program
    {
        public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
            .Build();

        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(Configuration)
                .CreateLogger();

            try
            {
                Log.Warning("Starting BuildWebHost");

                BuildWebHost(args).Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseSerilog()
                .Build();
    }
}

Startup.cs

启动.cs是纯默认设置.我没有对新的Asp.Net Core MVC项目的原始模板进行任何更改.

Startup .cs is pure default. I haven't altered it in any way from the original template for the new Asp.Net Core MVC project.

我以为我可能需要在IServiceCollection服务中添加Serilog,但是 ="https://nblumhardt.com上的精简,卑鄙的ASP.NET Core 2日志记录/2017/08/use-serilog/"rel =" noreferrer> https://nblumhardt.com/2017/08/use-serilog/告诉我这是不必要的.

I have thought that I might need to add Serilog in the IServiceCollection services, but the article Leaner, meaner ASP.NET Core 2 logging at https://nblumhardt.com/2017/08/use-serilog/ tells me it's unnecessary.

然后,您可以继续删除所有其他的记录器配置 徘徊:无需在日志记录"部分中 appsettings.json,任何位置都没有AddLogging(),也没有配置 通过Startup.cs中的ILoggerFactory.

You can then go ahead and delete any other logger configuration that’s hanging around: there’s no need for a "Logging" section in appsettings.json, no AddLogging() anywhere, and no configuration through ILoggerFactory in Startup.cs.

UseSerilog()替换内置的ILoggerFactory,以便每个记录器 在您的应用程序中,无论是Serilog的Log类,Serilog的 将支持ILogger或Microsoft.Extensions.Logging.ILogger 具有相同的Serilog实现,并通过相同的方式进行控制 配置.

UseSerilog() replaces the built-in ILoggerFactory so that every logger in your application, whether it’s Serilog’s Log class, Serilog’s ILogger, or Microsoft.Extensions.Logging.ILogger, will be backed with the same Serilog implementation, and controlled through the same configuration.

我期望的结果

通过解决方案中任何项目的任何类中的构造函数简单地插入记录器的能力.例如:

The ability to simply inject a logger via a constructor in any class in any project in the solution. For example:

using Serilog;
public MyTestClass(ILogger logger) { ... }

我得到的内容-网络(启动)项目

如果使用Microsoft.Extensions.Logging包装器,则注入在HomeController中有效:

Injection works in the HomeController if I use the Microsoft.Extensions.Logging wrapper:

using Microsoft.Extensions.Logging;

public class HomeController : Controller
{
    ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
        _logger.LogDebug("Controller instantiated.");
    }
}

如果我尝试注入Serilog.ILogger,则在任何项目/类中注入都会失败

Injection fails in any project/class if I try to inject Serilog.ILogger

using Serilog;
using Serilog.Extensions.Logging; // Not sure if this would help.

public class HomeController : Controller
{
    ILogger _logger;

    public HomeController(ILogger logger)
    {
        _logger = logger;
        _logger.Debug("Controller instantiated.");
    }
}

InvalidOperationException:无法解析服务 尝试激活时输入"Serilog.ILogger"类型 'ObApp2.Web.Mvc.Controllers.HomeController'.

InvalidOperationException: Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'ObApp2.Web.Mvc.Controllers.HomeController'.

我的大问题-其他项目

更大的问题是,如果我正在解决方案中的另一个项目中工作,我将无法通过任何尝试通过DI的方式来获得记录器.

My bigger issue is that I can't get a logger via DI through any method I've tried if I'm working in another project in the solution.

当我尝试使用Microsoft.Extensions.Logging或使用Serilog进行注入时,在构建时会遇到丢失的参数异常.

When I try to inject either using Microsoft.Extensions.Logging or using Serilog I get a missing parameter exception at build time.

using Microsoft.Extensions.Logging;
namespace ObApp.Domain
{
    public class MyTestClass(ILogger<MyTestClass> logger) { ... }

- or -

using Serilog;
namespace ObApp.Domain
{
    public class MyTestClass(ILogger logger) { ... }

两者都会生成类似于以下内容的构建错误:

Both generate build error similar to:

没有给出与所需形式相对应的参数 MyTestClass.MyTestClass(ILogger)的参数"logger"

There is no argument given that corresponds to the required formal parameter 'logger' of 'MyTestClass.MyTestClass(ILogger)'

问题

  1. 使用这种类型的Serilog配置进行注入时,建议我在进行注入的类文件中引用Microsoft.Extensions.Logging或Serilog吗?

  1. When injecting with this type of Serilog configuration, is it recommended that I reference Microsoft.Extensions.Logging or Serilog in the class files where I'm doing the injection?

如何让DI在所有项目中都能正常工作?

How can I get DI to work through all projects?

推荐答案

对我有用的一种方法:

我在ConfigureServices方法中使用AddSingleton()方法添加了Serilog.Core.Logger的实例.这样就解决了DI问题.此步骤代替了在StartUp构造函数中将Logger实例分配给Log.Logger的步骤.

I added an instance of Serilog.Core.Logger using the AddSingleton() method in the ConfigureServices method. This resolved the DI issue. This step replaces the step of assigning a Logger instance to Log.Logger in the StartUp constructor.

services.AddSingleton((ILogger)new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.File(<...>)
            .CreateLogger());

还更改类文件中的引用,使其指向Serilog.Ilogger

Also change references in your class files to point to Serilog.Ilogger

这篇关于将Asp.Net Core 2注入用于带有多个项目的Serilog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆