zoukankan      html  css  js  c++  java
  • Log4net的使用

    1、NuGet上下载log4net

    2、App.config中添加配置信息

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      <log4net>
        <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
        <!-- Set root logger level to ERROR and its appenders -->
        <root>
          <level value="ALL" />
          <appender-ref ref="SysAppender" />
        </root>
        <!-- Print only messages of level DEBUG or above in the packages -->
        <logger name="TestLogger">
          <level value="ALL" />
        </logger>
        <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
          <!-- 日志目录 -->
          <param name="File" value="Log/" />
          <param name="AppendToFile" value="true" />
          <param name="RollingStyle" value="Date" />
          <!-- 日志命名 -->
          <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
          <param name="StaticLogFileName" value="false" />
          <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
        <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
          <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
      </log4net>

    3.在AssemblyInfo.cs中 或 日志类namespace上方添加如下代码

    //log4net从配置文件中读取配置
    [assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]

    (指定配置文件方法)

    //添加log4net信息
    [assembly: log4net.Config.DOMConfigurator(ConfigFile = "Log.config", Watch = true)]

    4.使用log4net

            private static ILog log = LogManager.GetLogger("TestLogger");
            static void Main(string[] args)
            {
                log.Debug("Debug日志");
                log.Info("Info日志");
                log.Warn("Warn日志");
                log.Error("Error日志");
            }

     5.MVC项目使用

    把上文中<section />节点放到<configSections></configSections>节点下;<log4net> 节点放在根节点下;在Global 文件中添加 log4net.Config.XmlConfigurator.Configure();

  • 相关阅读:
    (网络编程)基于tcp(粘包问题) udp协议的套接字通信
    (网络编程)理论 原理
    (网络编程) 介绍
    C++开源代码项目汇总
    Facial Landmark Detection
    人脸对齐ASM-AAM-CLM的一些总结
    Ello讲述Haar人脸检测:易懂、很详细、值得围观
    显示形状回归算法(ESR)代码介绍
    百度图像搜索探秘
    人脸特征点检测
  • 原文地址:https://www.cnblogs.com/MrZheng/p/8966725.html
Copyright © 2011-2022 走看看