0

I've got a logger which is using a file appender and a console appender. I've got one line in my code that I only want to go to the console appeander. Is there a way to do that without making a while new logger?

Alternatively, I could just make another logger that only used the console, but I can't seem to even get that done. All examples I can find use class or method names. I am trying to use a name I provided in the app.config.

Here is my appconfig:

  <log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <!-- Pattern to output the caller's file name and line number -->
        <conversionPattern value="%level [%thread] %date - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="All" />
      </filter>
    </appender>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="D:\Something\Log\IntegrationTests\IntegrationTests.log" />
      <appendToFile value="false" />
      <rollingStyle value="Size" />
      <maximumFileSize value="250MB" />
      <maxSizeRollBackups value="20" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level [%thread] %date - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="All" />
      </filter>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="Console" />
      <appender-ref ref="RollingFile" />
    </root>
    <logger name="consoleOnly">
      <level value="ALL" />
      <appender-ref ref="Console" />
    </logger>
  </log4net>

Here is a line where I try to get "consoleOnly" Logger.

ILog log = LogManager.GetLogger("consoleOnly");
log.Debug("Poop");

Is that how I do it?

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
  • You can filter log messages for specific loggers (e.g. http://www.claassen.net/geek/blog/2009/06/log4net-filtering-by-logger.html) – RB. Mar 15 '18 at 15:00
  • Not what I want. Might as well create another logger with only a console appender, but can't even figure out how to grab one by name in the config and code. Was hoping there was some handy method on the loggger thats already in use. – Christopher Pisz Mar 15 '18 at 15:12
  • While, if you desperately want to do it in code, you could always use `Console.WriteLine` to write your message. However, I really would suggest filters is the way to go (unless you can explain a bit more context around what exactly you are trying to achieve - based on your current question, appender filters *is* the correct answer from a log4net perspective). – RB. Mar 15 '18 at 15:17
  • might help: https://stackoverflow.com/questions/36706713/log4net-logging-to-multiple-defined-appenders-using-class-name-loggers – FrankM Mar 15 '18 at 15:18
  • A filter, if I understand right, either logs a message that meets the criteria of that filter or doesn't. No? you don't apply filters at the appender level, but at the logger level, right? – Christopher Pisz Mar 15 '18 at 15:25

1 Answers1

0

ILog log = LogManager.GetLogger("consoleOnly");

log.Debug("Poop");

This should work fine. but I think you need to set additivity to false, otherwise it would also log using the root logger.

eg.

<logger name="consoleOnly" additivity="false">
   <level value="ALL" />
   <appender-ref ref="Console" />
</logger>
Community
  • 1
  • 1
sgmoore
  • 15,694
  • 5
  • 43
  • 67