Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am trying to log the info/errors using log4net.


What I have tried:

have added this to assemblyinfo.cs file

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

and Config file look like this

<?xml version="1.0" encoding="utf-8" ?>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      <level value="INFO" />     
      <appender-ref ref="RollingFileAppender" />
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <file value="c:\\LogFiles\\info.log" />      
      <staticLogFileName value="true" />
      <appendToFile value="true" />      
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="1MB" />     
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />

and calling the log method like below

public static class Logger
        private static readonly log4net.ILog log =
        public static void LogError(String message)
            log.Info("Application is working");

what might be the problem?
Updated 20-Feb-23 8:05am

Thanks for all the code samples. I created a console project to replicate and had no issues. The folder didn't exist to begin with on my machine it was created and log files wrote to it just fine.

I would suggest looking to see that your app has proper permissions to c:\logfiles directory.

Also another option would be to add

      <add key="log4net.Internal.Debug" value="true"/>

To your .config and then see what debug messages are being generated by log4net.
Share this answer
Replace the line:

by below:

log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900