Microsoft.Extensions.Logging.Log4Net
A couple of extension methods for adding log4net support to ASP.NET Core 1.1.
{
"Log4Net": {
"ConfigFileRelativePath": "log4net.xml",
"Repository": "NETCoreRepository"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="%property{appRoot}\app.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Add an extra line in the Startup.cs
constructor to tell it where to find the log4net XML file:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
// Configure log4net
env.ConfigureLog4Net(Configuration.GetSection("Log4Net"));
// ...
}
// ...
}
Make a call to loggerFactory.AddLog4Net
inside of the Configure
method in Startup.cs
.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Verbose;
loggerFactory.AddConsole();
loggerFactory.AddDebug();
// Register Log4Net
loggerFactory.AddLog4Net(Configuration.GetSection("Log4Net"));
// ...
}
Now you will be able to use the Microsoft Logging framework throughout your application, and log4net will be used as a logging provider (based on the configuration provided in log4net.xml
).
public class HomeController : Controller
{
private readonly ILogger _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public void Index()
{
_logger.LogInformation("This will get written to app.log via log4net.");
}
}