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?
Still looking for a solution? I got mine from this link .
All I had to do was add this two lines of code at the top of "public static void Main" method in the "program class".
Yes, you have to add:
You can also configure your asp.net core application in such a way that everything that is logged in the output console will be logged in the appender of your choice. You can also download this example code from github and see how i configured it.
Following on Irfan's answer, I have the following XML configuration on OSX with .NET Core 2.1.300 which correctly logs and appends to a
./log
folder and also to the console. Note thelog4net.config
must exist in the solution root (whereas in my case, my app root is a subfolder).Another note, the traditional way of setting the XML up within
app.config
did not work:For some reason, the log4net node was not found when accessing the XMLDocument via
log4netConfig["log4net"]
.There is a third-party log4net adapter for the ASP.NET Core logging interface.
Only thing you need to do is pass the
ILoggerFactory
to yourStartup
class, then calland have a config in place. So you don't have to write any boiler-plate code.
More info here
You need to install the Microsoft.Extensions.Logging.Log4Net.AspNetCore NuGet package and add a log4net.config-file to your application. Then this should work:
I am successfully able to log a file using the following code
log4net.config in website root
I've figured out what the issue is the namespace is ambigious in the loggerFactory.AddLog4Net(). Here is a brief summary of how I added log4Net to my Asp.Net Core project.
Add the log4net.config file in your root application folder
Open the Startup.cs file and change the Configure method to add log4net support with this line loggerFactory.AddLog4Net
First you have to import the package using Microsoft.Extensions.Logging; using the using statement
Here is the entire method, you have to prefix the ILoggerFactory interface with the namespace