0

I've been shown how to use log4net and got a config file to use. I lost this config file and now I cant get it to work. For some reason the guy who configured it could not get it to work while having the configuration info in app.config so he placed it in log4net.config in the output folder.

He then called this when the program started up:

        var logConfig = Path.Combine(Path.GetDirectoryName(typeof(App).Assembly.Location), "log4net.config");
        var configStream = new FileInfo(logConfig);
          log4net.Config.XmlConfigurator.Configure(configStream);

And it worked flawlessly.

Now I got it to work perfectly in a new console project. However allmost all of my projects are addons to another application so my project types are class libraries loaded by this other application (called Revit).

Anyway, so I know that I can log, but just cant get it to work... This is log4net.config that I use:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <file value="logs\" />
    <datePattern value="dd.MM.yyyy'.log'" />
    <staticLogFileName value="false" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="5MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFileAppender"/>
  </root>
</log4net>

Each class that I want to log stuff from declares the an ILog instance on the class level. Like this:

static private ILog _logger = LogManager.GetLogger(typeof(Program));

The ILog instance reports that all logging is enabled (debug and so on), but there are no files created.

Do I need to do anything else?

Im going nuts here! Thanks for any help!

Erik83
  • 539
  • 3
  • 5
  • 19

2 Answers2

1

It seems you tried a few different ways to configure log4net. If one fails, the best option is to enable log4net debugging to see if your logger is working/crashed. In your app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>

...

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

Log4net FAQ

Peter
  • 27,590
  • 8
  • 64
  • 84
  • Hmm, I was thinking about a way to see whats going on with log4net. It doesnt throw any exceptions or anything in the normal mode. This solved my problem. Thanks! The actual problem was that I was loading the settings at the wrong time in my addin so that the host application dropped it before I started logging information. – Erik83 Jan 14 '15 at 19:49
0

In App.config file you should add this code in order to register the log4net section handler and the log will be generated

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

or Check if your AssemblyInfo.cs file has this code :

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)] 
prasy
  • 250
  • 1
  • 14
  • Just tried it, and nothing happened. My log4net config is not in the app.config, its in a separate file. It seems to work without it in my console project. My app.config has copy to output to always and build action: none. – Erik83 Jan 07 '15 at 21:58
  • Check if you have the following code in your AssemblyInfo.cs file: [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)] – prasy Jan 07 '15 at 22:08
  • @Erik83: You can follow this [link](http://mitch-wheat.blogspot.com/2007/04/log4net-net-logging-tool.html). I followed the same link and it worked perfectly for me – prasy Jan 07 '15 at 22:11
  • Make sure that your log4net.config file is set with the following properties: Build Action: Content Copy to output directory: Copy Always – prasy Jan 07 '15 at 22:20
  • I just checked that. I also made sure that the file actually sits in the output folder, and it does. – Erik83 Jan 07 '15 at 22:22
  • Check this [Question](http://stackoverflow.com/questions/445976/log4net-config-in-external-file-does-not-work) , it may help you – prasy Jan 07 '15 at 22:26
  • Something really wierd happened now. I got it to work, however it logs to 2 different locations. I believe that the there where some problems with the folder that I used. I tested C:\Test\ and C:\Temp and now its seems like its using an old config file along with the new. Somehow it stored it and I cant get it to store all logs at one location... – Erik83 Jan 07 '15 at 22:58
  • Probably you should check the new and old config and try to set only on file location..but that we were able to see the logs – prasy Jan 08 '15 at 19:42