Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...