简体   繁体   English

如何将文本写入log4net文件

[英]How to write text into log4net file

I have a C# application. 我有一个C#应用程序。 I would like to integrate a log File. 我想集成一个日志文件。 Then I try do this in App.config 然后我尝试在App.config中执行此操作

<log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="logfile.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>
    <appender name="InfoFileAppender" type="log4net.Appender.FileAppender">
      <file value="info_logfile.txt" />
      <appendToFile value="true" />

    </appender>
    <appender name="ErrorFileAppender" type="log4net.Appender.FileAppender">
      <file value="error_logfile.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="FileAppender" />
      <level value="INFO" />
      <appender-ref ref="InfoFileAppender" />
      <level value="ERROR" />
      <appender-ref ref="ErrorFileAppender" />
    </root>
  </log4net>

So I try to write in infoFile: 所以我尝试在infoFile中编写:

log.Info("chiamata json");

but I don't see any text in info_logfile.txt 但我在info_logfile.txt中看不到任何文本

Where is my error? 我的错误在哪里?

Can we help me? 我们可以帮我吗?

Reguards 护卫队

Do you instantiate your log file? 您实例化日志文件吗? Try the following to see that it works with a console appender. 请尝试以下操作,以了解它可与控制台附加程序一起使用。 (this example is copied from David Saltner) (此示例摘自David Saltner)

using log4net;
using log4net.Config;

public class LogTest
{
    private static readonly ILog logger = 
           LogManager.GetLogger(typeof(LogTest));

    static void Main(string[] args)
    {
        BasicConfigurator.Configure();

        logger.Debug("Here is a debug log.");
        logger.Info("... and an Info log.");
        logger.Warn("... and a warning.");
        logger.Error("... and an error.");
        logger.Fatal("... and a fatal error.");
    }
}

.

private static readonly ILog logger = 
          LogManager.GetLogger(typeof(LogTest));

This creates a logger for the class LogTest. 这将为LogTest类创建一个记录器。 You don't have to use a different logger for each class you have, you can use different loggers for different sections or packages within your code. 您不必为每个类使用不同的记录器,可以对代码中的不同部分或包使用不同的记录器。 The class of the logger is output to the log so you know where any logged information has come from. 记录器的类将输出到日志,因此您知道记录的信息来自何处。

BasicConfigurator.Configure();

This method initializes the log4net system to use a simple Console appender. 此方法初始化log4net系统以使用简单的控制台附加程序。 Using this allows us to quickly see how log4net works without having to set up different appenders. 使用此功能,我们可以快速查看log4net的工作原理,而不必设置其他附加程序。

This should get you started. 这应该使您入门。 Check out this great article for a brief introduction on how to set up appenders and more. 请查看这篇出色的文章,以简要介绍如何设置附加程序等。

If the configuration is in your app.config file you need to add the sections in the config file: 如果配置在app.config文件中,则需要在配置文件中添加以下部分:

<configSections>
 <section name="log4net" 
   type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, 
         Culture=neutral, PublicKeyToken=1b44e1d426115821" />
</configSections>

It is better practice to configure your log4.net is a log4net.config file. 更好的做法是将log4.net配置为log4net.config文件。 Then you need to add the following line to your assembly which reads your configuration file: 然后,您需要将以下行添加到读取您的配置文件的程序集中:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

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

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