4

I'm using log4net in order to create a log, but it doesn't do anything.

Here is the app.config:

<?xml version="1.0" encoding="utf-8">
<configuration>
    <configSection>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSection>
    <log4net>
        <appender name="WriteToFile" type="log4net.Appender.FileAppender">
            <file value="log.txt" />
            <layout ="log4net.Layout.SimpleLayout" />
        </appender>
        <root>
            <level value="ALL" />
            <appender-ref ref="WriteToFile"/>
        </root>
    </log4net>
</configuration>

I have the following line in AssemblyInfo.cs:

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

Here is an attempt to write to the file:

private static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public void write()
{
    log.Info("Some text here");
}

And the following line:

log4net.Config.XmlConfigurator.Configure();
Venu Saini
  • 427
  • 2
  • 6
  • 19
Golan Kiviti
  • 3,895
  • 7
  • 38
  • 63

2 Answers2

9

If this is an executable I suppose that your config file is not called App.config at the resulting bin folder but rather MyApp.exe.config. So you may try fixing this:

ConfigFile ="App.config"

You can also skip the config file name if you are using the default:

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

Also make sure that the account you are running your application under has write permission to the folder in which you expect the log file to be written.


UPDATE:

Step by step guide:

  1. Create a new Console Application
  2. Install the log4net NuGet
  3. Use the following in your App.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
        </configSections>
        <log4net>
            <appender name="WriteToFile" type="log4net.Appender.FileAppender">
                <file value="log.txt" />
                <layout type="log4net.Layout.SimpleLayout" />
            </appender>
            <root>
                <level value="ALL" />
                <appender-ref ref="WriteToFile" />
            </root>
        </log4net>
    </configuration>
    
  4. And in your Program.cs:

    using log4net;
    using System.Reflection;
    
    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    
    namespace ConsoleApplication1
    {
        class Program
        {
            private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
            static void Main(string[] args)
            {
                log.Info("Some text here");
            }
        }
    }
    
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Tried both ways and still doesnt work :( got another idea? – Golan Kiviti Dec 13 '15 at 07:34
  • I have updated my answer with a step by step guide to a working example. I am not quite sure if it was a typo in your post or rather you have this in your actual code, but the XML configuration file you showed is invalid. For example the XML declaration is missing a closing `?`, also this is invalid: ``, ... – Darin Dimitrov Dec 13 '15 at 07:48
  • Hmm, weird, those very same steps worked perfectly fine for me. Which version of `log4net` are you using? I used the latest one: 2.0.5 – Darin Dimitrov Dec 13 '15 at 07:56
  • I tested for version 2.0.8 and it works. I run the app as admin. – zapoo May 16 '17 at 14:45
2

I have only been successful with log4net when I ran the static Configure method:

        log4net.Config.XmlConfigurator.Configure();

Wrapping it in one method below read default config for the application, be it web.config or app.config:

    private static void InitLogging()
    {
        log4net.Config.XmlConfigurator.Configure();
        _logger = Common.Logging.LogManager.GetLogger(typeof(Program));
        _logger.Debug("Logging initialized");
    }

It seems more elegant with an decorator attribute the way you are suggesting, but the above mentioned may be worth a try.

faester
  • 14,886
  • 5
  • 45
  • 56