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