1

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.

divinediu
  • 423
  • 1
  • 9
  • 33
  • Are you sure that your application does have permissions to write to the folder C:\Users\SOIS\Documents\Visual Studio 2010\WebSites\DummyPharmacy? Check the output during application execution, log4net should put any error information into the output console. – Georgy Smirnov Mar 27 '14 at 06:40
  • @GeorgySmirnov Thanks. Checked output. Edited my question with the error. – divinediu Mar 27 '14 at 06:50
  • That was so long ago:( I would suggest configure log4net in global.asax – Georgy Smirnov Mar 27 '14 at 07:17
  • @GeorgySmirnov Where do I place this? `[assembly: log4net.Config.XmlConfigurator(ConfigFile="log4net.config", Watch = true)]` – divinediu Mar 27 '14 at 07:22
  • You wouldn't need to do that, just follow the first answer here http://stackoverflow.com/questions/10204171/configure-log4net-in-web-application – Georgy Smirnov Mar 27 '14 at 07:27
  • `void Application_Start(object sender, EventArgs e){log4net.Config.XmlConfigurator.Configure();}` That was in place all along. I just cant figure out where the issue is! – divinediu Mar 27 '14 at 07:30
  • you mean that you configure log4net in two places (assembly.cs and global.asax)? – Georgy Smirnov Mar 27 '14 at 07:33
  • Yes, though I don't think anything is calling Assembly.cs. – divinediu Mar 27 '14 at 08:28

1 Answers1

1

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.

Fung
  • 3,508
  • 2
  • 26
  • 33
  • I did this in my Assembly.cs. It did not work. However, I need to add that this was a file I just created because the tutorial needed it. Anywhere else I should be placing this? – divinediu Mar 27 '14 at 06:54
  • The tutorial suggest you to put the log4net section in Web.config, not a separate file. In that case you don't need to modify the attribute. – Fung Mar 27 '14 at 07:05
  • 1
    I edited my answer with the current status. It's still executing but not logging. – divinediu Mar 27 '14 at 07:26