0
votes

This is my first attempt at logging, and I have researched similar questions, but cant seem to figure out my situation, probably because I am not getting any errors to go off of.

I am trying to use Log4Net to simply create a .txt file. I do not have a web.config or app.config, so I created my own config file to setup Log4Net(Through research, I found that I should be able to do this). The config file is below:

Log4Net.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,  log4net"/>
</configSections>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\myFolder\TestLog.txt" />//******Nothing Is Being Created Here****
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
  </layout>
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration> 

In my AssemblyInfo.cs

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

And the call to the logger

....
private static readonly ILog logger = LogManager.GetLogger("ExportLogger");
....
 log4net.Config.XmlConfigurator.Configure();//I know this shouldnt be done here, just testing
 logger.Debug("Successfully logged something");

The above code all runs without error, so I am not sure where my mistake is. I suspect I have the configuration setup incorrectly. Can anyone see what I am doing wrong and why there is no creation of the .txt file?

1
check write access rights of application pool user to your folder 'C:\myFolder' - Sergey
Assuming it isn't just permissions - as you're using a standalone log4net config file, you should probably remove the <configuration> and <configSections> elements, as they are only applicable to app/web.config files. Also use the overload of log4net.Config.XmlConfigurator.Configure() that lets you pass in that file. You can check LogManager.GetRepository().Configured to see if the configuration has been loaded. If it still doesn't work, add a trace listener to the debug output - stuartd
Did you make sure the log4net.dll is available in the bin directory? - Nick Prozee
@NickProzee Yes the dll is there - Reeggiie

1 Answers

0
votes

The error in your attribute:

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

You define the configuration file, but you are using the app.config for the configuration.

This will fix your problem:

[assembly: log4net.Config.XmlConfigurator()]