Log4Net不在发布模式下写入日志-控制台应用程序 [英] Log4Net doesn’t write log in release mode - Console Application

查看:256
本文介绍了Log4Net不在发布模式下写入日志-控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,并且有一个包装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

我的开发环境是

  • Visual Studio 2013和.NET Framework 4.5

控制台应用程序

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

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

课程库

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);
        }
    }
}

我已经关注了这篇文章,但没有帮助我弄清楚为什么Log4Net不在发布模式下写入日志文件吗?

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运行以发布模式构建的.Net 4.0 Windows应用程序时不会记录日志

推荐答案

有一些解决方法.

  • 您可以添加[MethodImpl(MethodImplOptions.NoInlining)] 属性添加到类库中的Logger构造函数方法中.

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

您可以在控制台项目(不是类库项目)中的AssemblyInfo.cs中添加[assembly: log4net.Config.XmlConfigurator(Watch = true)]

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

您可以在log4net.Config.XmlConfigurator.Configure(); Logger构造函数的开始.

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

我认为发生这种情况的原因是,在发布模式下,您的类库是内联的,因此,当log4net尝试查找该属性时,它认为调用程序集是不包含该属性的exe.

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.

我想您知道Logger类将意味着您将失去按方法名称进行过滤的功能,因为log4net仅会看到Logger.Info方法名称.

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.

这篇关于Log4Net不在发布模式下写入日志-控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆