1

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 marked "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.

Chad
  • 265
  • 1
  • 14
  • Is the .log4net file named according to your .exe? e.g. myapp.exe.log4net – TheNextman Jul 28 '11 at 13:30
  • No, is it suppose to be? I saw examples like that but nothing said it "had to be". I assumed it was just a convention. – Chad Jul 28 '11 at 13:33
  • Tried it just for kicks, same result: no log messages. – Chad Jul 28 '11 at 13:34
  • Documentation says it must be: "ConfigFileExtension If specified, this is the extension for the configuration file. The assembly file name is used as the base name with the this extension appended. For example if the assembly is loaded from the a file TestApp.exe and the ConfigFileExtension property is set to log4net then the configuration file name is TestApp.exe.log4net. This is equivalent to setting the ConfigFile property to TestApp.exe.log4net." (http://logging.apache.org/log4net/release/manual/configuration.html) – TheNextman Jul 28 '11 at 14:04
  • What's the setup of your solution? A single .exe? No project references? – TheNextman Jul 28 '11 at 14:05
  • It is a single solution with two projects: an exe and a dll. Both projects have the config values in the assembly and have their own log4net config files. – Chad Jul 28 '11 at 14:30

1 Answers1

0

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
Community
  • 1
  • 1
TheNextman
  • 12,428
  • 2
  • 36
  • 75