Log4net Not Creating Log File

后端 未结 6 980
栀梦
栀梦 2020-12-15 03:34

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

相关标签:
6条回答
  • 2020-12-15 04:08

    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!!!

    0 讨论(0)
  • 2020-12-15 04:09

    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.

    0 讨论(0)
  • 2020-12-15 04:15

    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]

    0 讨论(0)
  • 2020-12-15 04:20

    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.

    0 讨论(0)
  • 2020-12-15 04:33

    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)]

    0 讨论(0)
  • 2020-12-15 04:33

    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)

    0 讨论(0)
提交回复
热议问题