zoukankan      html  css  js  c++  java
  • log4net 控制台程序 写日志

    添加包:

     添加 log4net.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <!-- This section contains the log4net configuration settings -->
      <log4net>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
        </appender>
    
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
          <file value="log-file.log" />
          <appendToFile value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
    
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
          <!--<file value="logfile" />-->
          <param name= "File" value= "logfile"/>
          <appendToFile value="true" />
          <rollingStyle value="Composite" />
          <staticLogFileName value="false" />
          <datePattern value="yyyyMMdd'.log'" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="2MB" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
    
        <!-- Setup the root category, add the appenders and set the default level -->
        <root>
          <level value="ALL" />
          <appender-ref ref="ConsoleAppender" />
          <appender-ref ref="FileAppender" />
          <appender-ref ref="RollingLogFileAppender" />
        </root>
    
      </log4net>
    </configuration>

    使用:

    static void Main(string[] args)
            {
                ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
                XmlConfigurator.Configure(repository, new FileInfo("log4net.xml"));
                ILog log = LogManager.GetLogger(repository.Name, "NETCorelog4net");
                log.Info("log");
                log.Error("error");
                log.Warn("warn");
                var UserName = "admin";
                var dataIntercept = "" +
                    $"【当前操作用户】:{ UserName} 
    " +
                    $"【携带的参数有】: 接受消息 
    ";
                
                log.Info(dataIntercept);
    
                Console.Read();
            }

    效果:

     

  • 相关阅读:
    HDU 2045 不容易系列之(3)—— LELE的RPG难题(递归/动态规划)
    UVALive 6885 spfa
    hdu 1142 A Walk Through the Forest spfa
    hdu 2795 Billboard 线段树
    hdu 1394 Minimum Inversion Number 线段树求逆序数
    hdu 1754 I Hate It 线段树-区间最值
    poj 1088 滑雪 动态规划
    poj 1163 The Triangle 动态规划
    hdu 1258 Sum It Up 搜索
    hdu 3006 The Number of set 状态压缩
  • 原文地址:https://www.cnblogs.com/shuaichao/p/13825368.html
Copyright © 2011-2022 走看看