Configuring log4net programmatically, but extra logging going to console

Viewed 565

I've taken the code from Can you configure log4net in code instead of using a config file? and put it into my little NET5 application. The log file, thus far, seems okay, but as I haven't configured any appender going to the console, but only the one rolling file appender, I wasn't expecting to get anything going to the console. However, that is exactly what is happening - my messages are going to the console albeit in a different format, it's also not using my pattern layout.

This is what I have for setup at the moment:

    private void SetupLog4Net()
    {
        var hierarchy = (Hierarchy)LogManager.GetRepository();
        hierarchy.Root.RemoveAllAppenders();
        hierarchy.Root.Level = Level.Debug;
        
        var patternLayout = new PatternLayout
        {
            ConversionPattern = "%date{yyyy-MM-dd HH:mm:ss.fff} [%logger] %message%newline%exception",
        };
        patternLayout.ActivateOptions();
        
        var rollingFileAppender = new RollingFileAppender
        {
            AppendToFile = true,
            File = @"Logs/uav.log",
            Layout = patternLayout,
            MaxSizeRollBackups = 5,
            MaxFileSize = 1_000_000,
            RollingStyle = RollingFileAppender.RollingMode.Size,
            StaticLogFileName = true,
            Encoding = System.Text.Encoding.UTF8,
        };
        rollingFileAppender.ActivateOptions();
        hierarchy.Root.AddAppender(rollingFileAppender);

        hierarchy.Root.Level = Level.Info;
        hierarchy.Configured = true;
        BasicConfigurator.Configure(hierarchy);
    }

This is called as the first thing in the first thing that Main does (construct the application object, this is called via that constructor), before we get to absolutely anything else.

Then, later, when we call to log stuff, e.g.,

       var logger = LogManager.GetLogger(this.GetType());
       logger.Info(message.Message, message.Exception);

I get output such as:

1780 [6] INFO MyClass.MainApp (null) - Ready

I can't even make sense of half of that, but making sense isn't the point - clearing out this appender is. Comments on the actual configuration are fine as comments, but the main question is just how to remove that console output.

Using Log4NET 2.0.12 and .NET 5.0.400 on Linux

2 Answers

According to the log4net configuration page, calling BasicConfigurator.Configure() method will "Set up a simple configuration that logs on the console."

Seeing as you've configured things programmatically to write to a file, no need to include the line:

BasicConfigurator.Configure();

OK: so your REAL question is "How do I disable Log4Net console logging?"

  1. Remember: in Java, "log4j"is "the logger". In .Net, however, Log4net coexists with Microsoft.Extensions.Logging. So there's "extra stuff", which might yield "surprises". .Net itself (e.g. Net 5, nee ".Net Core") might also yield "surprises".

  2. Specifically, there are several solutions discussed here:

How do I disable log4net status messages to the console?

a. Change log4net.Config.BasicConfigurator.Configure(); to log4net.Config.XmlConfigurator.Configure();

b. set debug = false

c. Modify "log4net.Internal.Debug" in your app config file (e.g. "appsettings.json")

<appSettings>
   <add key="log4net.Internal.Debug" value="false"/>

Also look at the Log4Net FAQs:

http://logging.apache.org/log4net/release/faq.html

Internal debugging messages are written to the console and to the System.Diagnostics.Trace system. If the application does not have a console the messages logged there will be lost. Note that an application can redirect the console stream by setting the System.Console.Out.

As log4net internal debug messages are written to the System.Diagnostics.Trace system it is possible to redirect those messages to a local file.

Related