0
votes

I have a C# project that references another C# project. They both have app.config files that contain log4net configuration settings. When I run my code, only the log file for the main project appears, and the referenced project's log entries appear in the main project's log file.

Here's my app.config for my MainProject:

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

...

<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="MainProject.log" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="5" />
        <maximumFileSize value="100MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level | %date{ISO8601} | %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

My main project is an Azure cloud instance project, so it has one class that defines the WorkerRole. The log4net initialization code looks like this:

//Using statements
//...
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace MainProject
{
    public class WorkerRole : RoleEntryPoint
    {
        private static readonly log4net.ILog log4netLogger = log4net.LogManager.GetLogger(typeof(WorkerRole));

//...
log4netLogger.Info("This is a MainProject log entry")
//...

My referenced project is a class library with one class. Here's the app.config for my ReferencedProject:

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

...

<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="ReferencedProject.log" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="5" />
        <maximumFileSize value="100MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level | %date{ISO8601} | %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

The log4net initialization code in the ReferencedProject's one class looks like this:

//Using statements
//...
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace ReferencedProject
{
    public class ReferencedProjectClass : IDisposable
    {
        private static readonly log4net.ILog log4netLogger = log4net.LogManager.GetLogger(typeof(ReferencedProjectClass));

//...
log4netLogger.Info("This is a ReferencedProject log entry")
//...

When I run this code locally, the only logfile that gets generated is MainProject.log. That logfile has entries from both the MainProject and the ReferencedProject. It looks like this:

INFO | 2017-05-30 07:49:38,419 | This is a ReferencedProject log entry
INFO | 2017-05-30 07:49:38,421 | This is a MainProject log entry 

There's likely something I don't understand about how LogManager.GetLogger manages loggers. Can someone point out what I'm doing wrong?

1

1 Answers

2
votes

Only one app.config can be used at runtime. You can't use different app.config per project.

And because you run the main project, it takes the app.config from the main project and your app.config for my ReferencedProject is just ignored.