开发者

Load log4net configuration from Assembly in Mono

开发者 https://www.devze.com 2023-03-23 18:26 出处:网络
I am developing a C# application in Mono and trying to use log4net. The logger works just fine when I load the configuration manually however, I would like something more elegant.

I am developing a C# application in Mono and trying to use log4net. The logger works just fine when I load the configuration manually however, I would like something more elegant.

In the log4net documentation it states that a config can be loaded from the assembly by using the following (or similar) line:

[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]

I have ensured the config file gets moved to the build directory and has that extension. I have also made sure the AssemblyInfo.cs is mar开发者_运维知识库ked "Application Definition" in MonoDevelop. I do not know why this is not working. Does anybody have any ideas?

BTW: I have searched all over the place trying to find an answer. I also tried loading the config from a resource and that did not want to work either.


It is possible, but awkward, to use multiple config files with log4net. See the discussion here: log4net - configure using multiple configuration files

Are you doing logging within your .exe and your .dll?

Can you make do with a single config file?

Bear in mind this section from the documentation: "if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked."

Try and distill the issue down to a simple example.

  • Ensure your config file is name exename.exe.log4net
  • Ensure that you place the AssemblyAttribute you have in the quesion, into the AssemblyInfo.cs of your .exe
  • Ensure that you make a call to LogManager.GetLogger ASAP in your .exe, before invoking any code from your .DLL
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号