Question

I followed logging instructions given here:

I created a Assembly.cs that has this the following:

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

This is my web.config file:

<configuration>

    <configSections>
        <section name="log4net"
                 type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
<log4net>
        <appender name="FileAppender"
                  type="log4net.Appender.FileAppender">
            <file value="C:\Users\SOIS\Documents\Visual Studio 2010\WebSites\DummyPharmacy\logfile.txt" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="FileAppender" />
        </root>
    </log4net>
</configuration>

My connection class where I use logger has this: using log4net;

public class Connection
{
  private static readonly ILog log = LogManager.GetLogger(typeof(Connection));

The debug shows execution of the logging. No file created in my folder though. What went wrong?

Execution

My output file has this:

log4net:ERROR Failed to find configuration section 'log4net' 
in the application's .config file.Check your .config file 
for the <log4net> and <configSections> elements. The configuration
section should look like: <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

I have made a separate log4net.config file. Because editing the existing one does not allow me to define a element.

Was it helpful?

Solution

This is because you didn't tell log4net to read config from your log4net.config file in your assembly attribute:

see the Configuration Attributes in official document:

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.

Change it to following should work:

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

Or if you prefer a single .config file, then keep the attribute and move the configruations from log4net.config to Web.config.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top