views:

159

answers:

3

I created a simple szenario using Log4net, but it seems that my appenders does not work because the messages are not added to the logfile.

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 added:

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

and within the Application_Start()-Method:

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

what did I wrong?

Thanks in Advance Johannes

+1  A: 

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
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
@john84 - are you getting exceptions? Have you looked at the event logs?
Oded
There is one entry but it does not seem to be related with Log4net...
john84
A: 

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.

bechbd
Instead of sending everyone to another website, why not post a relevant excerpt?
Oded
Thanks I've checked the FAQ but these couldn't resolve my problem.
john84
+1  A: 

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