【问题标题】:Map ILogger to Serilog's Logger after ASP.NET Core 2 logging updateASP.NET Core 2 日志更新后将 ILogger 映射到 Serilog 的 Logger
【发布时间】:2017-08-30 07:59:33
【问题描述】:

来自@nblumhardt的post:

然后您可以继续删除任何其他挂起的记录器配置:appsettings.json 中不需要“记录”部分,任何地方都不需要 AddLogging(),也不需要通过 Startup.cs 中的 ILoggerFactory 进行配置。

当using Serilog; 和ILogger 在我的控制器中时出现异常。

private readonly ILogger _logger;

public CustomersController(ILogger logger)
{
    _logger = logger;
}

结果:

处理请求时发生未处理的异常。
InvalidOperationException:尝试激活“Customers.Api.Controllers.CustomersController”时无法解析“Serilog.ILogger”类型的服务。

我还需要在Startup.ConfigureServices() 方法中向 DI 提供一些信息吗?

据我所知,我的程序课程遵循帖子中的说明。

public class Program
{
    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();

        BuildWebHost(args).Run();
    }

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

【问题讨论】:

    标签: c# asp.net-core serilog


    【解决方案1】:

    将预期类型从 ILogger logger 更改为 ILogger&lt;CustomersController&gt; 记录器:

    private readonly ILogger _logger;
    
    public CustomersController(ILogger<CustomersController> logger)
    {
        _logger = logger;
    }
    

    【讨论】:

    【解决方案2】:

    如果您仍希望使用 Serilog 自己的 ILogger 而不是 ASP.NET Core 的 ILogger&lt;T&gt;,另一种选择是将 Serilog 记录器注册到您的 IoC 容器。

    在 https://github.com/nblumhardt/autofac-serilog-integration 有一个 Autofac 集成,其他容器也可能有集成包。

    【讨论】:

      【解决方案3】:

      您可以按照以下代码使用。

      //using Microsoft.Extensions.Logging;
      
      public class Startup
          {
              public Startup(IConfiguration configuration)
              {
                  Configuration = configuration;
              }
      
              public IConfiguration Configuration { get; }
      
              // This method gets called by the runtime. Use this method to add services to the container.
              public void ConfigureServices(IServiceCollection services)
              {
                  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
              }
      
              // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
              public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
              {
                  if (env.IsDevelopment())
                  {
                      app.UseDeveloperExceptionPage();
                  }
                  else
                  {
                      app.UseHsts();
                  }
      
                  app.UseHttpsRedirection();
                  app.UseMvc();
                  //Logger for File
                  loggerFactory.AddFile("FilePath/FileName-{Date}.txt");
      
              }
          }
      
          //Add following code into Controller:
      
          private readonly ILogger<ControllerName> _log;
      
          public ControllerConstructor(ILogger<ControllerName>logger)
           {
                _log = logger;
           }
          [HttpGet]
          public ActionResult GetExample()
          {
              try
              {
                  //TODO:Log Informationenter code here
                  _log.LogInformatio("LogInformation");
              }
              catch (Exception exception)
              {
                   //TODO: Log Exception
                   _log.LogError(exception, exception.Message + "\n\n");
              } 
              return view("viewName");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-26
        • 2019-02-07
        • 1970-01-01
        • 2018-05-14
        • 2018-04-14
        • 1970-01-01
        • 2020-08-08
        相关资源
        最近更新 更多