简体   繁体   English

以编程方式配置 log.net,但额外的日志记录将转到控制台

[英]Configuring log4net programmatically, but extra logging going to console

I've taken the code from Can you configure log.net in code instead of using a config file?我从中获取了代码你能用代码而不是使用配置文件配置 log.net 吗? and put it into my little.NET5 application.并将其放入我的 little.NET5 应用程序中。 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.这被称为 Main 所做的第一件事(构建应用程序 object,这是通过该构造函数调用的),在我们得到绝对其他任何东西之前。

Then, later, when we call to log stuff, eg,然后,稍后,当我们打电话记录东西时,例如,

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

I get output such as:我得到 output 例如:

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.我什至无法理解其中的一半,但理解不是重点——清除这个 appender 才是。 Comments on the actual configuration are fine as comments, but the main question is just how to remove that console output.对实际配置的评论很好,但主要问题是如何删除控制台 output。

Using Log.NET 2.0.12 and .NET 5.0.400 on Linux在 Linux 上使用 Log.NET 2.0.12 和 .NET 5.0.400

According to the log.net configuration page, calling BasicConfigurator.Configure() method will "Set up a simple configuration that logs on the console."根据log.net配置页面,调用BasicConfigurator.Configure()方法将“设置一个在控制台上登录的简单配置”。

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?"好的:所以您真正的问题是“如何禁用 Log4Net 控制台日志记录?”

  1. Remember: in Java, "log4j"is "the logger".请记住:在 Java 中,“log4j”是“记录器”。 In.Net, however, Log.net coexists with Microsoft.Extensions.Logging.然而,在 .Net 中,Log.net 与 Microsoft.Extensions.Logging 共存。 So there's "extra stuff", which might yield "surprises".所以有“额外的东西”,这可能会产生“惊喜”。 .Net itself (eg Net 5, nee ".Net Core") might also yield "surprises". .Net 本身(例如 Net 5,旧称“.Net Core”)也可能产生“惊喜”。

  2. Specifically, there are several solutions discussed here:具体来说,这里讨论了几种解决方案:

How do I disable log.net status messages to the console?如何禁用控制台的 log.net 状态消息?

a. A。 Change log.net.Config.BasicConfigurator.Configure();更改log.net.Config.BasicConfigurator.Configure(); to log.net.Config.XmlConfigurator.Configure();log.net.Config.XmlConfigurator.Configure();

b. b. set debug = false设置调试=假

c. Modify "log.net.Internal.Debug" in your app config file (eg "appsettings.json") c. 在您的应用程序配置文件中修改“log.net.Internal.Debug”(例如“appsettings.json”)

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

Also look at the Log4Net FAQs:另请查看 Log4Net 常见问题解答:

http://logging.apache.org/log.net/release/faq.html http://logging.apache.org/log.net/release/faq.html

Internal debugging messages are written to the console and to the System.Diagnostics.Trace system.内部调试消息被写入控制台和 System.Diagnostics.Trace 系统。 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.请注意,应用程序可以通过设置 System.Console.Out 重定向控制台 stream。

As log.net internal debug messages are written to the System.Diagnostics.Trace system it is possible to redirect those messages to a local file.由于 log.net 内部调试消息被写入 System.Diagnostics.Trace 系统,因此可以将这些消息重定向到本地文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM