Question

I'm getting an error on a program that has log4net configured, but no log file is being created. I'm certain the logging is set up in the program because other users have created log files from the same, using an identical config file to what I'm posting below (except for the filepath). I'm sure that I have write permissions to the path. At the point where the program fails, it absolutely must have passed the initialization of the logging.

Does anything look wrong in this config file, or has anybody experienced a similar issue and know something I should look for within the program I'm trying to get a log from?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="AutoTag" type="System.Configuration.NameValueSectionHandler"/>
    <section name="WindwardReports" type="System.Configuration.NameValueSectionHandler"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821"/>
  </configSections>

  <AutoTag>
    <add key="_debug" value="true"/>
  </AutoTag>

  <WindwardReports>
    <add key="line.break" value="internal"/>
  </WindwardReports>

  <appSettings>
    <add key="sql.timeout" value="240"/>
  </appSettings>

  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">

      <param name="File" value="C:\Users\loganm\Documents\Catapult.log"/>

      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="2"/>
      <param name="MaximumFileSize" value="100KB"/>
      <param name="RollingStyle" value="Size"/>
      <param name="StaticLogFileName" value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] ac.server %-5p %c - %m%n"/>
      </layout>
    </appender>

    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
  </log4net>
</configuration>
Was it helpful?

Solution

Give the following code in your application before you put your logging code:

log4net.Config.XmlConfigurator.Configure();

You can define it in Global.asax:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup

    // Initialize log4net.
    log4net.Config.XmlConfigurator.Configure();
}

You can also add the following line as Kevin advised (either mentioning your config file name or not):

[assembly: log4net.Config.XmlConfigurator]

or

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

Hope it helps!!!

OTHER TIPS

make sure the log4net.config file, Copy to Output Directory property is not "Do not copy".

Actually best solution is adding

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

to track whats going on the back side. It will help you to detect issue.

Here are some possible things to consider:

  1. Maybe you're using a different log4net DLL version than your colleagues? Try removing the version and public key info from the config section, i.e. shorten it to:

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
    
  2. Try changing the filename to use escaped back slashes, i.e. "C:\\Users\\loganm\...". Alternatively, you could try using forward slashes: "C:/Users/loganm/..."

  3. If you rebuilt the program before running it, make sure you in fact are using the same AssemblyInfo.cs as your colleagues. One common mistake is to neglect to initialize the logger there. It should have something like this at the bottom:

    // Log4Net activation
    [assembly: log4net.Config.XmlConfigurator]

I had this issue also, amazing how much time I spent checking everything, finally a query on Google reminded me of a step I took when I last used Log4Net: I had to add this to the AssemblyInfo.cs file! Crazy! I am using Log4Net in a C# Console application with the Log4Net settings in the app.config file.

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

access denied in asp.net user could be an issue. eg: running from test project does logging but running from web application won't

just add this to config

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

debug the application and watch the standard output. this will output log4net debug info (removed once issue is found out)

My problem was solved when I set App.config (and log4net.xml) to copy to output directory.

enter image description here

In my case I was missing the part with the reference to the appender name in my log4net.config file. I was missing this:

<appender-ref ref="RollingFileAppender"/>

It should look like this for every appender name you are using:

<root>
  <level value="DEBUG" />
    <appender-ref ref="AppenderName#1" />
    <appender-ref ref="AppenderName#2" />
</root>

I found the problem, and unfortunately it was just that logging had not actually been set up in the program and I was given incorrect information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top