A log4net provider for Microsoft.Extensions.Logging, the logging subsystem used by ASP.NET Core.
This package routes ASP.NET log messages through log4net, so you can get information about ASP.NET's internal operations logged to the same log4net appenders as your application events.
First, install the apache.log4net.Extensions.Logging NuGet package into your web or console app.
Install-Package apache.log4net.Extensions.Logging
(Note: You should use version 1.x for .NET Core 1.x projects and version 2.x for .NET Core 2.x projects because of breaking change in .NET Core logging API introduced in .NET Core 2.0)
Next, create a log4net.config in the root of your project, see log4net.config for examples. You can also use the config included with the sample app
Finally (.NET Core 1.x), in your Startup
class's Configure()
method, remove the existing logger configuration entries and
call AddLog4Net()
on the provided loggerFactory
.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerfactory,
IApplicationLifetime appLifetime)
{
loggerfactory.AddLog4Net();
Finally (.NET Core 2.x), in your Startup
class's ConfigureServices()
method, remove the existing logger configuration entries and
call AddLog4Net()
on the ILoggerBuilder
in AddLogging
.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder => builder.AddLog4Net());