0

I'm facing a strange problem.

In a solution, I have a shared assembly info file (solution file) and linked it beside the regular AssemblyInfo.cs into my projects. Properties are set to build. Attributes that are shared between all project are in the SharedAssemblyInfo.cs file. Attributes specific to the project are in AssemblyInfo.cs.

Now when it comes to configuring log4net, the configuration file is only read when placing the log4net attribute in the SharedAssemblyInfo.cs. When I put it into AssemblyInfo.cs log4net will not initialize.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

This makes no sense to me. I would understand that somehow if it only would work in AssemblyInfo.cs. Thought it could be the order of compilation or the order how the files are included in the csproj file, but this doesn't seem to matter.

Any idea?

assembly info files

martinoss
  • 5,268
  • 2
  • 45
  • 53

1 Answers1

0

Just found the root cause for the behavior.

I'm having assemblies like these:

Job.exe -> Shared.dll

In Shared.dll, I have a logging service that calls LogManager.GetLogger(). It seems, the log4net assembly attribute needs to be in the assembly that first calls LogManager.GetLogger(). When I first call GetLogger() from Job.exe, Log4Net is initialized properly even if there is no log4net assembly attribute in Shared.dll.

Also found this in the docs:

Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore 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.

If you configure Log4Net with this method, you run into the same issue when getting the logger from an assembly other than the entry assembly.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net configSource="log4net.config" />
  
  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
martinoss
  • 5,268
  • 2
  • 45
  • 53