john84
john84

Reputation: 2451

Log4net does not write the log in the log file

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 following to the web.config file:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>        
</configSections>

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
            <file value="D:\MyData\Desktop\LogFile.txt" />
            <appendToFile value="true" />
            <encoding value="utf-8" />
            <layout type="log4net.Layout.SimpleLayout" />
    </appender>


    <root>
        <level value="INFO" />
        <appender-ref ref="LogFileAppender" />
    </root>
</log4net>

Within the global ASAX file I have added:

ILog logger = LogManager.GetLogger(typeof(MvcApplication));

And within the Application_Start method:

logger.Info("Starting the application...");

Why the test log "Starting the application..." is not being added to the log file?

Upvotes: 170

Views: 181857

Answers (13)

RotatingWheel
RotatingWheel

Reputation: 1051

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/

Upvotes: 0

Ayub
Ayub

Reputation: 2375

Also, Make sure the "Copy always" option is selected for [log4net].config

enter image description here

Upvotes: 42

Gurunadh Duvvuru
Gurunadh Duvvuru

Reputation: 388

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.

Upvotes: 2

Mahmut EFE
Mahmut EFE

Reputation: 5248

Your config file seems correct. Then, you have to register your Log4net config file to application. So you can use below code:

var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));

After registering process, you can call below definition to call logger:

private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

log.Error("Sample log");

Upvotes: 0

Prajakta Kale
Prajakta Kale

Reputation: 391

Make sure the following line code should be there in AssemblyInfo.cs file.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

and also check for this line in Application_start() method.

log4net.Config.XmlConfigurator.Configure();

Upvotes: 1

Md Rahatur Rahman
Md Rahatur Rahman

Reputation: 3244

In my case I had to give the IIS_IUSRS Read\write permission to the log file.

Upvotes: 4

Jay Byford-Rew
Jay Byford-Rew

Reputation: 6014

For me I moved the location of the logfiles and it was only when I changed the name of the file to something else it started again.

It seems if there is a logfile with the same name already existing, nothing happens.

Afterwards I rename the old file and changed the log filename in the config back again to what it was.

Upvotes: 3

zanussi
zanussi

Reputation: 1316

In my case, log4net wasn't logging properly due to having a space in my project name. Drove me nuts why the same code worked just fine in a different project, but didn't in the new one. Spaces. A simple space.

So, beware spaces in project names. I've learned my lesson.

Upvotes: 2

shay
shay

Reputation: 2141

Insert:

 [assembly: log4net.Config.XmlConfigurator(Watch = true)]

at the end of AssemblyInfo.cs file

Upvotes: 22

LCJ
LCJ

Reputation: 22652

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)]

Upvotes: 40

bechbd
bechbd

Reputation: 6341

Use this FAQ page: Apache log4net Frequently Asked Questions

About 3/4 of the way down it tells you how to enable log4net debugging by using application tracing. This will tell you where your issue is.

The basics are:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

And you see the trace in the standard output

Upvotes: 58

Andreas Paulsson
Andreas Paulsson

Reputation: 7803

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();
}

Upvotes: 337

Oded
Oded

Reputation: 498942

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.

Upvotes: 26

Related Questions