0

I am struggling to get anything to log out using log4net. I have tried using code from various sources but still no joy. Can someone please help me figure out why?

Assembly.cs

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

MyClass.cs

private static readonly ILog log = LogManager.GetLogger(typeof(MyClass));

log.Info("Complete: "+result); // used when my "task" completes, is always called

log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <file value="log-file.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <header value="[Your Header text here]" />
      <footer value="[Your Footer text here]" />
      <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
    </layout>
  </appender>

  <!-- Set the default logging level and add the active appenders -->
  <root>
    <level value="DEBUG" />
    <appender-ref ref="LogFileAppender" />
  </root>

</log4net>

One thing to note is that all this is withing one project. The program running this is in a different project but all logging is done within this assembly.

decPL
  • 5,384
  • 1
  • 26
  • 36
Chris
  • 26,744
  • 48
  • 193
  • 345

3 Answers3

1

You can enable log4net's internal debug output, to track down why it's not logging.

See this answer for details.

Community
  • 1
  • 1
demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
1

Have you added a log4net section to your configuration in your Web.config or App.config?

<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
</configuration>
Jennifer S
  • 1,419
  • 1
  • 24
  • 43
0

The log4net documentation for assembly attributes says this:

Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

Your logging however is in an external assembly already. This is a bit of an issue when using assembly attributes, I would recommend using XmlConfigurator.ConfigureAndWatch(…) in your startup code instead.

stuartd
  • 70,509
  • 14
  • 132
  • 163