7

I have a console application and have a class library which wraps the Log4Net methods. Now when run the application in debug mode it writes log but when it is built in release mode it doesn’t write log file. What would be the solution for this? The sample code and config file is given below

My development environment is

  • Visual Studio 2013 and .NET Framework 4.5

Console Application

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            log4net.GlobalContext.Properties["LogFileName"] = "TestLogin.txt";
            Logger log = new Logger(typeof(Program));
            log.Info("Logging is enabled!!");
        }
    }
}

App.config in Console Application

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <log4net>
      <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value ="%property{LogFileName}"/> 
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %level  - %message%newline%exception" />
        </layout>
      </appender>
      <root>
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender" />
      </root>
    </log4net>
</configuration>

Class Library

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Logging
{
    public class Logger
    {
        private readonly ILog log = null;

        public Logger(Type type)
        {
            log = LogManager.GetLogger(type);
        }
        public void Info(object message)
        {
            log.Info(message);
        }
    }
}

I have followed the post and it didn’t help me to figure out why Log4Net doesn’t write in log file in release mode?

log4net doesn't log when running a .Net 4.0 Windows application built in Release mode

Community
  • 1
  • 1
SnowWhite
  • 190
  • 1
  • 3
  • 9
  • I would enable log4net internal debugging to try to understand what's happening, as described here: http://logging.apache.org/log4net/release/faq.html – Joe Mar 18 '14 at 21:12

5 Answers5

7

There are a few workarounds for this.

  • You could add the [MethodImpl(MethodImplOptions.NoInlining)] attribute to your Logger constructor methods in your class library.

  • You could add [assembly: log4net.Config.XmlConfigurator(Watch = true)] to AssemblyInfo.cs in your Console Project (not the class library project)

  • You can add log4net.Config.XmlConfigurator.Configure(); at the start of your Logger constructor.

I think the reason this is happening is that in release mode, your class library is inlined and so when log4net attempts to find the attribute, it thinks the calling assembly is your exe which does not contain the attribute.

PS.

I presume you are aware that your Logger class will mean that you lose the ability to filter by method names, as log4net will only see the Logger.Info method name.

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • 1
    I tried last 2 options before I posted this question. I tried the first option and it don't work unfortunately. – SnowWhite Mar 19 '14 at 14:38
  • Most strange. I tried all three options and they all worked. (And without them no logging occurred in release mode). Are you sure that the config file is in your release folder and is correct? – sgmoore Mar 19 '14 at 14:58
  • 2
    Thank you very much sgmoore!!! I was always checking in Debug folder and not in release folder. My bad!!! Yes it logs in release mode. I didn't have to do anything. The code I listed above works itself, so no extra step is required to make it work. the only thing you need to do is to check in release folder. – SnowWhite Mar 19 '14 at 15:22
  • I wanted to vote up but it requires 15 reputation points whereas I do have only 9 points :( – SnowWhite Mar 19 '14 at 15:24
  • 1
    There is a point to be made. If you run the above code through visual studio then log will be created in release mode but if you go to the release folder and then click the ConsoleApplication1.exe then you will see that no log is created. To solve this problem follow one of the workarounds sgmoore suggested. I followed the second one which is adding this line of code log4net.Config.XmlConfigurator.Configure() in Logger's constructor. Now build the solution and go to the release folder and click the ConsoleApplication1.exe again and you see that log is created. – SnowWhite Mar 19 '14 at 15:37
1

The line [assembly: log4net.Config.XmlConfigurator(Watch = true)] should be added to your AssemblyInfo.cs file in the Properties folder.

enter image description here

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • 1
    Did you try that in release mode? this is a good code and you should be able to run this pretty quick without any fuss. I have tried before I posted this. It didn't work. I have tried again, its not working. – SnowWhite Mar 18 '14 at 20:45
  • here is my AssemblyInfo.cs [assembly: log4net.Config.XmlConfigurator(Watch = true)] [assembly: AssemblyTitle("Logging")] [assembly: AssemblyDescription("")] – SnowWhite Mar 18 '14 at 20:46
  • I have similar code, only I use `private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);` instead of creating a `Logger` class etc – Christian Phillips Mar 18 '14 at 21:05
  • well, i do have a seperate class and that is the requirement unfortunately. – SnowWhite Mar 19 '14 at 14:45
1

For users with an MVC app that already have the lines [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] in AssemblyInfo.cs and log4net.Config.XmlConfigurator.Configure(); in Global.asax.cs, but it's still not writing, make sure that the Application Pool user has permissions to write to the location where you're writing the log.

To get around this, I simply created a log directory on the IIS server NOT inside inetpub, gave it adequate permissions (I suppose it can be "Everyone", "Full Control" if it's just a child log directory and you're feeling lazy), and wrote the full path in the log4net.config (or Web.config, if you didn't isolate it).

Here's my config file:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:/absolute/path/to/logfile.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maximumFileSize value="10MB"/>
      <datePattern value="yyyyMMdd'-FULL.log'" />
      <maxSizeRollBackups value="-1"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>
J.D. Mallen
  • 4,339
  • 3
  • 22
  • 33
1

Make sure that the log4net.config file is added in your final release binary and corresponds to the path mentioned in ConfigFile = "log4Net.config".

I had the same problem, then I realized that I was simply missing out this config file in my release binary.

V.Aggarwal
  • 557
  • 4
  • 12
0

I have this in my Logging.dll and my Program.exe assembly.cs. Then it works in both debug and release mode. My program is a windows service.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Rahbek
  • 1,331
  • 14
  • 9