zoukankan      html  css  js  c++  java
  • Log4.net示例

    //先用Nuget安装最新Log4net
    using System;
    using System.IO;
    using log4net;
    using log4net.Config;
    using log4net.Repository;
    
    namespace ConsoleApp
    {
        public class Program
        {
            static void Main(string[] args) {
    
                ILoggerRepository repository = LogManager.CreateRepository("repository");
    
                //默认简单配置,输出到控制台
                //BasicConfigurator.Configure(repository);
                XmlConfigurator.Configure(repository, new FileInfo("log.config"));
    
                ILog log = LogManager.GetLogger(repository.Name, "log");
                log.Info("info");
                log.Error("error");
    
                Console.ReadLine();
            }
        }
    
    }

    log.config配置文件

    <?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/" />
          <appendToFile value="true" />
          <rollingStyle value="Composite" />
          <staticLogFileName value="false" />
          <datePattern value="yyyyMMdd'.log'" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="1MB" />
          <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>

    引用地址 https://www.cnblogs.com/linezero/p/log4net.html

  • 相关阅读:
    浙大版《C语言程序设计(第3版)》题目集 --总结
    | C语言I作业09
    c语言课本及pta作业中运用到的程序思维
    | C语言I作业08
    团队作业(四):描述设计
    实验三《敏捷开发与XP实践》_实验报告
    MyOD(课下作业,选做)
    实验二《面向对象程序设计》_实验报告
    20175226 2018-2019-2《java程序设计》结对编程-四则运算(第二周-阶段总结)
    20175226 类定义
  • 原文地址:https://www.cnblogs.com/kerwincui/p/9139813.html
Copyright © 2011-2022 走看看