2

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

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • Q: The problem is that you're getting an extraneous log message saying "Ready" ... and you'd like to suppress it. Everything else is working as you expect. Correct? Q: Could you please show us *more* of the "extraneous messages"? Perhaps that might give us a clue where they're coming from ... and how to turn them off. – FoggyDay Aug 16 '21 at 00:32
  • The "Ready" message is a valid message, but I'm expecting it solely in the "Logs/uav.log" log file. It shows up in there, but also shows up on the console. In fact, everything that goes to the log file (correctly!) also is duplicated, in a different format, on the console, whereas the goal is for the console to be empty. – Tanktalus Aug 16 '21 at 01:41
  • 1
    Hey mate, try removing line "BasicConfigurator.Configure(hierarchy);" I don't think you'll need that if you aren't writing to console. – fuzzy_logic Aug 16 '21 at 04:39
  • It would be great that, if you use SeriLog in .NET 5 . It is more tthan modern and you can use there more functionality for seriLog. https://www.infoworld.com/article/3624022/how-to-use-advanced-serilog-features-in-aspnet-core-mvc.html – Cəfərov Murad Aug 15 '21 at 22:44
  • I don't recall saying I was building an ASP application. I'm not. Also, I use the Log4* series across multiple languages, don't normally have this problem, and like to keep that consistent as much as possible. Further, this "answer" will not be helpful to the average person who may search for this. – Tanktalus Aug 15 '21 at 22:51
  • @fuzzy_logic That did it, thanks. Post it as an answer so I can mark it correct. Thanks! – Tanktalus Aug 21 '21 at 01:37

2 Answers2

2

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();
fuzzy_logic
  • 896
  • 11
  • 14
1

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.

FoggyDay
  • 11,962
  • 4
  • 34
  • 48