Manish Patel
Manish Patel

Reputation: 4491

why log4net config file is not being read

I have a file log4net.xml in the root of my solution. The file property for copying to bin is set to Always copy. I have confirmed that the file copies to the bin directory.

Within the config I have set up a file appender (basically a copy paste from the documentation):

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
 <file value="Logs\\log.log" />
 <appendToFile value="true" />
 <rollingStyle value="Date"/>
 <datePattern value="yyyyMMdd-HHmmss" />
 <layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
 </layout>
</appender>

I also have this line in AssemblyInfo.cs

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

And loggers are set up like this in code files:

private static ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

I added log4net.Config.XmlConfigurator.Configure(); (and also with FileInfo parameter) to the main Program.cs too.

However if I execute the program the expected log file is not created. I suspect log4net isn't even reading the config file.

Am I missing something? Executing from within VS also doesn't work (though somehow it is picking up the Console Appender part).

Similar posts:

Thanks in advance.

EDIT

So I got this working in VS by deleting a copy/pasted part from the documentation:

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

However, running from the exe still doesn't work

Upvotes: 5

Views: 3535

Answers (4)

Hoshiyar Rawat
Hoshiyar Rawat

Reputation: 1

For the Console application. we can use to configure using the following code

      // Path of your log4net config file
      string  configFilePath = @"/log4net.config";

        XmlDocument log4netConfig = new XmlDocument();
        log4netConfig.Load(File.OpenRead(configFilePath));

        var repo = LogManager.CreateRepository(
            Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));

        log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);

Upvotes: 0

Manish Patel
Manish Patel

Reputation: 4491

I worked this out, I don't know if it's the proper fix - and I have no idea why it worked once in VS - but I think it's just that the config file is not configured properly.

Firstly, since there is a declaration of the file in AssemblyInfo.cs there is no need for log4net.Config.XmlConfigurator.Configure();.

Secondly, I simply had to add both appenders to the root tag:

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

And that's it. VS outputs on console as well as file, and exe outputs to file.

Upvotes: 0

Win
Win

Reputation: 62300

Easiest way is to debug log4net error is to dump them to a file under C:\tmp\log4net.txt. This won't answer your original question, but it will give you a clue where to look for.

How do I enable log4net internal debugging?

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

<configuration>
    ...

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

    ...
</configuration>

Upvotes: 4

Tim
Tim

Reputation: 28530

I think the issue might be with the double \\ in your path in the config file. Given that when you run the program in Visual Studio and the Console Appender part works, that would seem to indicate that the config file is being read, it just can't write to the file because the path isn't valid. I'm not that familiar with Log4Net, so I don't know if an invalid path would throw an exception or not - it might be getting swallowed.

Try changing the file value in your config to one backslash, like this:

<file value="Logs\log.log" />

Upvotes: 0

Related Questions