I configure log4net
in my asp.net core 2.0 application as mentioned in this article LINK
program.cs
public static void Main(string[] args)
{
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
BuildWebHost(args).Run();
}
HomeController
public class HomeController : Controller
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController));
public IActionResult Error()
{
log.Info("Hello logging world!");
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
log4net.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
<appender name="RollingFile" type="log4net.Appender.FileAppender">
<file value="C:\Temp\app.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Unlucky!, I didn't see any file generated in C:\Temp\app.log
directory. What would be the mistake? how to configure log4net
for asp.net core 2.0?
Click here to learn how to implement log4net in .NET Core 2.2
The following steps are taken from the above link, and break down how to add log4net to a .NET Core 2.2 project.
First, run the following command in the Package-Manager console:
Then add a log4net.config with the following information (please edit it to match your set up):
Then, add the following code into a controller (this is an example, please edit it before adding it to your controller):
Then call the relevant controller action (using the above example, call
/Values/Get
with an HTTP GET), and you will receive the output matching the following: