2

I am using the following app.config settings for multiple log files. The following are my app.config settings. But when I use "LogFileAppender" settings to write log to a file, it also writing the logs to the Console

<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <File value="C:\File1.log" />
    <AppendToFile value="true" />
    <rollongStyle  value="Composite" />
    <maximumFileSize value="200MB" />
    <layout type="log4net.Layout.PatternLayout">
        <ConversionPattern value="%date %messge%newline"/>
    </layout>
    </appender>

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
        <File value="C:\File2.log" />
        <AppendToFile value="true" />
        <rollongStyle  value="Date" />
        <maximumFileSize value="100MB" />
        <layout type="log4net.Layout.PatternLayout">
            <ConversionPattern value="%date %messge%newline"/>
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <acceptOnMatch value="true" />
            <levelMin value="INFO" />
        </filter>
        <filter type="log4net.Filter.LevelRangeFilter">
            <acceptOnMatch value="true" />
            <levelMin value="FATAL" />
        </filter>
    </appender>

    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
        <level value ="ERROR"/>
        <foreColor value ="Red"/>
    </mapping>
    <mapping>
        <level value ="WARN"/>
        <foreColor value ="Yellow"/>
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
        <ConversionPattern value="%date %messge%newline"/>
    </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />
        <appender-ref ref="RollingFile" />
        <appender-ref ref="ColoredConsoleAppender" />
    </root>
</log4net>

In the C# code, I am using the below method calls.

    private static readonly log4net.ILog ilogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    log4net.Config.XmlConfigurator.Configure();

Question:

How can I restrict the Console logging while using the LogFileAppender using the above app.config file?

Ullan
  • 1,311
  • 8
  • 21
  • 34
  • Have you tried removing it? – Vsevolod Goloviznin Dec 08 '14 at 14:57
  • I don't understand what you mean by **when I use "LogFileAppender" settings** - in your config file you are using all 3 apenders. Can you explain? – ShayD Dec 08 '14 at 15:00
  • Three applications(2 dlls and one exe) sharing the same configuration file. But in my application, just I want to log using LogFileAppender – Ullan Dec 08 '14 at 15:32
  • If I remove console appender, then it will work. But it is used other applications – Ullan Dec 08 '14 at 15:33
  • 1
    Take the Console appeneder out of the section and explicitly create it in the sections of code where you need it. You won't be able to give it an arbitrary name that way, but such is the trade-off. – Duston Dec 08 '14 at 15:37
  • I have explicitly created the loggers and it worked. In this case, the other applications that need to be modified little bit – Ullan Dec 08 '14 at 15:42

2 Answers2

1

Add a ForwardingAppender in your configuration between the root logger and the ColoredConsoleAppender, so that you get this configuration

  • root -> LogFileAppender
  • root -> RollingFile
  • root -> ForwardingAppender -> ColoredConsoleAppender

Then in your ForwardingAppender add a filter that will only let pass events that do not originate from the application you don't want; a LoggerMatchFilter would be a good idea since you can exclude loggers originating from the namespace of the desired application.

samy
  • 14,832
  • 2
  • 54
  • 82
0

I found the solution by modifying the app.config as following.

<logger name = "FileLogger" additivity="false">
   <level value="ALL" />
   <appender-ref ref="LogFileAppender" />
</logger>

<root>
   <level value="ALL"/>
   <appender-ref ref="RollingFile"/>
   <appender-ref ref="RollingFileAppender" />
   <appender-ref ref="ColoredConsoleAppender" />
</root>

In the C# code, call the LogFileAppender explicitly as below

private static readonly log4net.ILog ilogger = log4net.LogManager.GetLogger("FileLogger");
Ullan
  • 1,311
  • 8
  • 21
  • 34