3

I am a Log4Net newbie and I am having some problems in order to log my application log messages in a log file. I think something is wrong with Web.config file configuration. This is a snippet of what I have in web.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

<log4net>
    <root>
    </root>

    <logger name="Tomahawk" additivity="False">
        <level value="ALL" />
        <appender-ref ref="MyFileAppender" />
      </logger>


    <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
      <file value="application.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline" />
      </layout>
    </appender>

  </log4net>

Moreover, I included the following line in AssemblyInfo.cs:

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

In C# code, ALL I do is this:

ILog log = LogManager.GetLogger("Tomahawk");
log.info("Some debug info...");

But nothing happens. Nothing is being logged in the log file.

Does anyone have a clue about this issue?

stuartd
  • 70,509
  • 14
  • 132
  • 163
mhenley
  • 69
  • 1
  • 6
  • Check the value of `LogManager.GetRepository().Configured` to see if log4net is picking up the configuration - there are some cases where an assembly attribute doesn't pick it up correctly, e.g. if the attribute isn't in the startup project. If it is configured, check the permissions on the file allow the program to write to it. – stuartd Feb 01 '17 at 16:43
  • Hello, I checked the value according to your reply... and the value is False. A workaround that I found was call the Configure method of XmlConfigurator this way: log4net.Config.XmlConfigurator.Configure() in the application startup. Doing this... everthing worked fine. I am wondering why even including the atribute in XML web.config file it's not working... – mhenley Feb 02 '17 at 01:52
  • Attributes are neat but they can be picky - _"Using attributes can be a clearer method for defining where the application's configuration will be loaded from … 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".**_ – stuartd Feb 02 '17 at 10:19
  • Text above is from https://logging.apache.org/log4net/release/manual/configuration.html#attributes – stuartd Feb 02 '17 at 10:19

1 Answers1

1

Try the following:

Step 1 - Create a separate file called log4net.config

Step 2 - Remove these lines from your xml

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

Step 3 - Configure the root section similar to this:

<root>
    <level value="DEBUG" />
    <appender-ref ref="MyFileAppender" />
</root>

Step 4 - In your Program.cs include this:

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

Step 5 - In your Startup.cs class get your logger working with this global variable

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

Step 6 - Inside the Configure method log your info message like this

if (env.IsDevelopment())
{
    log.Info("Yeap! We are in development now! That's great!");

    app.UseDeveloperExceptionPage();
 }

Step 7 - Open your application.log file and Voilà!!

Celso Jr
  • 136
  • 10