I have created a simple scenario using Log4net, but it seems that my log appenders do not work because the messages are not added to the log file.
I added the followi
As @AndreasPaulsson suggested, we need to configure it. I am doing the configuration in AssemblyInfo
file. I specify the configuration file name
here.
// Log4Net Configuration.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Make sure the process (account) that the site is running under has privileges to write to the output directory.
In IIS 7 and above this is configured on the application pool and is normally the AppPool Identity, which will not normally have permission to write to all directories.
Check your event logs (application and security) to see if any exceptions were thrown.
In my case I had to give the IIS_IUSRS
Read\write permission to the log file.
There are a few ways to use log4net. I found it is useful while I was searching for a solution. The solution is described here: https://www.hemelix.com/log4net/
Do you call
log4net.Config.XmlConfigurator.Configure();
somewhere to make log4net read your configuration? E.g. in Global.asax:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// Initialize log4net.
log4net.Config.XmlConfigurator.Configure();
}
For me I had to move Logger to a Nuget Package. Below code need to be added in NuGet package project.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
See https://gurunadhduvvuru.wordpress.com/2020/04/30/log4net-issues-when-moved-it-to-a-nuget-package/ for more details.