mhenley
mhenley

Reputation: 69

Log4NET Logger class

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?

Upvotes: 3

Views: 1457

Answers (1)

Celso Jr
Celso Jr

Reputation: 295

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

Upvotes: 1

Related Questions