简体   繁体   English

log4net 1附加器2文件

[英]log4net 1 appender 2 files

Is there a possibility to have one appender for log4net that would log information to two files if it meets treshold level. 是否有可能有一个log4net附加程序,如果符合阈值级别,该附加程序会将信息记录到两个文件中。 It it is INFO level it will write everything besides debug if it is debug, it writes everything including debug and just keep those two. 它是INFO级别,如果是debug,它将写入除debug之外的所有内容,它写入包括debug在内的所有内容,并保留这两个内容。 I could do it with two appenders, however I want to have only one variable that would figure out based on treshold level to which log file write, to only one, or to both. 我可以使用两个追加程序来完成此操作,但是我只希望有一个变量可以根据日志文件写入的阈值级别,仅写入一个或同时写入两个变量来确定。

  <appender name="Name" type="log4net.Appender.RollingFileAppender,log4net"> <file value="Path" /> <treshold value="info" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <maximumFileSize value="2000KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <Header value="[Start]&#13;&#10;" /> <Footer value="[End]&#13;&#10;" /> <ConversionPattern value="%-5level %message%newline" /> </layout> </appender> 

You can configure log4net to have individual log messages write to two (or more) different files. 您可以将log4net配置为使单个日志消息写入两个(或更多)不同的文件。 You can even have each file use a different threshold value. 您甚至可以让每个文件使用不同的阈值。

Here is an example with two log files, one called basic.log that includes INFO or higher messages, and one called details.log that includes DEBUG or higher messages. 这是一个包含两个日志文件的示例,一个名为basic.log的文件包含INFO或更高级别的消息,一个名为details.log的文件包含DEBUG或更高级别的消息。 The sample shows that a log.Debug() call will have its output go to just the details.log file, and a log.Info() call will have its output in both files. 该示例显示log.Debug()调用的输出将仅进入details.log文件,而log.Info()调用的两个文件均将输出。

Here is the main program: 这是主程序:

class Program
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
    static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Debug("This is a debug message");
        log.Info("This is an info message");
    }
}

And the app.config file: 和app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
  </startup>

  <log4net>
    <appender name="BasicFile" type="log4net.Appender.RollingFileAppender, log4net">
      <file value="C:\temp\basic.log"/>
      <threshold value="INFO"/>
      <appendToFile value="True"/>
      <layout type="log4net.Layout.PatternLayout">
        <Header value="[Start]&#13;&#10;" />
        <Footer value="[End]&#13;&#10;" />
        <ConversionPattern value="%-5level %message%newline" />
      </layout>
    </appender>

    <appender name="DetailsFile" type="log4net.Appender.RollingFileAppender, log4net">
      <file value="C:\temp\details.log"/>
      <threshold value="DEBUG"/>
      <appendToFile value="True"/>
      <layout type="log4net.Layout.PatternLayout">
        <Header value="[Start]&#13;&#10;" />
        <Footer value="[End]&#13;&#10;" />
        <ConversionPattern value="%-5level %message%newline" />
      </layout>
    </appender>

    <root>
      <level value="DEBUG"/>
      <appender-ref ref="BasicFile"/>
      <appender-ref ref="DetailsFile"/>
    </root>
  </log4net>
</configuration>

When I run this, c:\\temp\\basic.log has just the INFO message, and c:\\temp\\details.log has both messages. 当我运行此命令时,c:\\ temp \\ basic.log仅包含INFO消息,而c:\\ temp \\ details.log同时具有这两个消息。

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

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