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

前端 未结 5 2155
清酒与你
清酒与你 2020-12-19 08:45

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 relea

5条回答
  •  伪装坚强ぢ
    2020-12-19 09:28

    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.

提交回复
热议问题