168

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?

Rahatur
  • 3,147
  • 3
  • 33
  • 49
john84
  • 2,431
  • 4
  • 24
  • 30

13 Answers13

334

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();
}
Andreas Paulsson
  • 7,745
  • 3
  • 25
  • 31
  • 4
    Thanks a Lot that's it, I had the following line: log4net.Config.BasicConfigurator.Configure(); Do I call this if I use the c# configuration instead of web.config to configure log4net? Is this "Configure"-Method call in any way required because in many tutorials I didn't find this line of code. – john84 Sep 02 '10 at 11:56
  • 9
    There are numerous ways to tell log4net where to look for configuration, see http://logging.apache.org/log4net/release/manual/configuration.html. Calling XmlConfigurator.Configure() will make log4net look for configuration in .config or web.config. BasicConfigurator.Configure will (according to SDK docs): "Initializes the log4net logging system using a ConsoleAppender that will write to Console.Out." – Andreas Paulsson Jun 28 '11 at 07:23
  • FYI - you can also use log4net.Config.BasicConfigurator if you do NOT want it file based. Output is sent to console. – silverArc Apr 24 '12 at 14:08
  • 2
    I am doing the configuration in `AssemblyInfo` file. Posted as an answer below. – LCJ Aug 09 '13 at 11:38
  • Argh, found so many of these questions and everywhere they fail to mention where to find the actual log files... Is it so obvious that everyone already knows? – MrFox Jan 17 '14 at 13:53
  • No. It is not obvious at all. Lack of intuitive use is Log4Net's biggst flaw. – Rhyous Apr 28 '15 at 20:13
55

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

TFD
  • 23,890
  • 2
  • 34
  • 51
bechbd
  • 6,206
  • 3
  • 28
  • 47
41

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

enter image description here

Ayub
  • 2,345
  • 27
  • 29
40

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)]
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 9
    what happens if i have both this and log4net.Config.XmlConfigurator.Configure(); on load? – n00b Nov 07 '13 at 23:41
  • 1
    @n00b, then we can comment this line, actually only with this way it worked to me "log4net.Config.XmlConfigurator.Configure();", And one more thing : I added this in my web.config settings too – himanshupareek66 Feb 15 '19 at 07:29
26

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks for your Help, also I thought about permission problems but also if I set on the folder read, write, modify, and execute permissions to everyone it does not work. – john84 Sep 01 '10 at 17:06
  • @john84 - are you getting exceptions? Have you looked at the event logs? – Oded Sep 01 '10 at 17:11
  • Also important, as I just discovered, with a Windows Service running under a specific account. – Bob Mc Jul 13 '15 at 18:56
  • Thanks a lot that helped me a lot. – Abbas Sep 01 '18 at 13:31
22

Insert:

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

at the end of AssemblyInfo.cs file

shay
  • 2,021
  • 1
  • 15
  • 17
  • This was my rescue. Doing a desktop Forms project and putting log4net.Config.XmlConfigurator.Configure(); in main was not helping. Thanks – Tomas Hesse Feb 21 '19 at 12:47
  • this helped me. I had configuration in web.config but log4net seemed to not pick up the changes done. I added "Watch=true" to that attribute and anithing started working – albe Dec 13 '19 at 13:21
4

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

Rahatur
  • 3,147
  • 3
  • 33
  • 49
3

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.

Jay Byford-Rew
  • 5,736
  • 1
  • 35
  • 36
2

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.

zanussi
  • 1,286
  • 2
  • 22
  • 29
  • 1
    Where did you have this issue? Log4Net is working great in one project, but not on another... don't know why. Tks – Pascal Aug 17 '17 at 22:52
1

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();
Prajakta Kale
  • 392
  • 3
  • 19
1

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.

Derek C.
  • 890
  • 8
  • 22
  • 1
    Try to avoid links as answers. They may not always work. Perhaps edit your answer to include a summary of the info in the link. – Derek C. May 07 '20 at 21:01
0

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");
Mahmut EFE
  • 5,137
  • 5
  • 46
  • 56
0

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/

RotatingWheel
  • 1,023
  • 14
  • 27