ASP.NET Core 2 日志记录更新后将 ILogger 映射到 Serilog 的记录器
Map ILogger to Serilog's Logger after ASP.NET Core 2 logging update
来自@nblumhardt's post:
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.
我在控制器中 using Serilog;
和 ILogger
时遇到异常。
private readonly ILogger _logger;
public CustomersController(ILogger logger)
{
_logger = logger;
}
结果:
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'Customers.Api.Controllers.CustomersController'.
我是否还需要在 Startup.ConfigureServices()
方法中向 DI 提供一些信息?
我的程序 class,据我所知,遵循 post.
中的说明
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();
}
将预期类型从 ILogger logger
更改为 ILogger<CustomersController>
记录器:
private readonly ILogger _logger;
public CustomersController(ILogger<CustomersController> logger)
{
_logger = logger;
}
如果您仍然希望使用 Serilog 自己的 ILogger
而不是 ASP.NET Core 的 ILogger<T>
,另一种选择是在您的 IoC 容器中注册 Serilog 记录器。
在 https://github.com/nblumhardt/autofac-serilog-integration 上有 Autofac 的集成,其他容器也可能有集成包。
您可以按照以下代码使用
//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");
}
来自@nblumhardt's post:
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.
我在控制器中 using Serilog;
和 ILogger
时遇到异常。
private readonly ILogger _logger;
public CustomersController(ILogger logger)
{
_logger = logger;
}
结果:
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'Customers.Api.Controllers.CustomersController'.
我是否还需要在 Startup.ConfigureServices()
方法中向 DI 提供一些信息?
我的程序 class,据我所知,遵循 post.
中的说明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();
}
将预期类型从 ILogger logger
更改为 ILogger<CustomersController>
记录器:
private readonly ILogger _logger;
public CustomersController(ILogger<CustomersController> logger)
{
_logger = logger;
}
如果您仍然希望使用 Serilog 自己的 ILogger
而不是 ASP.NET Core 的 ILogger<T>
,另一种选择是在您的 IoC 容器中注册 Serilog 记录器。
在 https://github.com/nblumhardt/autofac-serilog-integration 上有 Autofac 的集成,其他容器也可能有集成包。
您可以按照以下代码使用
//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");
}