Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net doesn’t write log in release mode - Console Application

Tags:

c#

.net

log4net

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

like image 424
SnowWhite Avatar asked Mar 18 '14 20:03

SnowWhite


1 Answers

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.

like image 94
sgmoore Avatar answered Nov 08 '22 17:11

sgmoore