Log4net does not write the log in the log file
Asked Answered
I

13

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?

Inclinometer answered 1/9, 2010 at 13:13 Comment(0)
S
335

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();
}
Sidwohl answered 1/9, 2010 at 21:15 Comment(6)
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.Inclinometer
There are numerous ways to tell log4net where to look for configuration, see logging.apache.org/log4net/release/manual/configuration.html. Calling XmlConfigurator.Configure() will make log4net look for configuration in <app.exe>.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."Sidwohl
FYI - you can also use log4net.Config.BasicConfigurator if you do NOT want it file based. Output is sent to console.Vale
I am doing the configuration in AssemblyInfo file. Posted as an answer below.Janitor
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?Seventeen
No. It is not obvious at all. Lack of intuitive use is Log4Net's biggst flaw.Tigges
E
57

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

Emilieemiline answered 1/9, 2010 at 13:38 Comment(7)
Thanks I've checked the FAQ but these couldn't resolve my problem.Inclinometer
Thanks to this answer I was able to identify that my root section was referring to the wrong logger!!Astilbe
great tip. I found that access was denied for asp.net userPaperboy
if it doesn't work to someone, then please try my way to use this: <log4net> <internal> <Debug value="true"> </Debug> </internal> </log4net>Toccaratoccata
this saved me x hours of headache.Philter
Thought I was losing my mind, this really saved me. (In my case I had a converter that changed namespace in a refactor and I wasn't aware of this, so lo4net wasn't finding it)Antipope
Very useful, it showed were our logs were really storedFrontal
J
41

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

enter image description here

Jackboot answered 19/8, 2015 at 12:51 Comment(1)
This was the fix for me. I always forget this setting.Rochelle
J
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)]
Janitor answered 4/2, 2013 at 8:25 Comment(2)
what happens if i have both this and log4net.Config.XmlConfigurator.Configure(); on load?Kalagher
@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 <add key="log4net.Internal.Debug" value="true"/>Toccaratoccata
V
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.

Velarize answered 1/9, 2010 at 13:16 Comment(4)
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.Inclinometer
@Inclinometer - are you getting exceptions? Have you looked at the event logs?Velarize
Also important, as I just discovered, with a Windows Service running under a specific account.Vtehsta
Thanks a lot that helped me a lot.Titan
O
22

Insert:

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

at the end of AssemblyInfo.cs file

Overdress answered 8/9, 2015 at 11:30 Comment(2)
This was my rescue. Doing a desktop Forms project and putting log4net.Config.XmlConfigurator.Configure(); in main was not helping. ThanksAbbie
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 workingLurlinelusa
H
4

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

Hamm answered 26/8, 2019 at 10:27 Comment(0)
P
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.

Pulsatile answered 10/4, 2017 at 8:49 Comment(0)
T
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.

Teenateenage answered 19/8, 2016 at 14:17 Comment(1)
Where did you have this issue? Log4Net is working great in one project, but not on another... don't know why. TksGillispie
H
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();
Hyaloplasm answered 25/9, 2019 at 9:59 Comment(0)
C
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.

Cyanamide answered 7/5, 2020 at 19:59 Comment(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.Electrophotography
G
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");
Gubernatorial answered 2/5, 2020 at 23:25 Comment(0)
C
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/

Conclusive answered 7/7, 2020 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.