Log4Net

简介: Log4Net

YFLog.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class YFLog
{
    private static readonly log4net.ILog logger = log4net.LogManager.GetLogger("WebLogger");
    public static void logInfo(string logstr)
    {
        Console.WriteLine(logstr);
        initLog4net();
        logger.Info(logstr);
    }
    public static void logInfo(string format, params object[] args)
    {
        Console.WriteLine(format, args);
        initLog4net();
        logger.InfoFormat(format, args);
    }
    public static void logError(string logstr)
    {
        Console.WriteLine(logstr);
        initLog4net();
        logger.Error(logstr);
    }
    public static void logError(string format, params object[] args)
    {
        Console.WriteLine(format, args);
        initLog4net();
        logger.ErrorFormat(format, args);
    }
    private static void initLog4net()
    {
        if (!logger.IsInfoEnabled)
        {
            var logCfg = new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            log4net.Config.XmlConfigurator.ConfigureAndWatch(logCfg);
        }
    }
}

log4net.config

<?xml version="1.0" encoding="utf-8" ?>  
<log4net>  
  <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">  
    <filter type="log4net.Filter.LevelMatchFilter">  
      <levelToMatch value="INFO" />  
    </filter>  
    <filter type="log4net.Filter.DenyAllFilter" />  
    <encoding value="utf-8"/>  
    <preserveLogFileNameExtension value="true" />  
    <appendToFile value="true" />  
    <rollingStyle value="Date" />  
    <file value="Logs\\" />  
    <StaticLogFileName value="false"/>  
    <datePattern value="yyyyMMdd'-info.log'" />  
    <layout type="log4net.Layout.PatternLayout">  
      <conversionPattern value="%date - %message%newline" />  
    </layout>  
  </appender>  
  <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">  
    <filter type="log4net.Filter.LevelMatchFilter">  
      <levelToMatch value="ERROR" />  
    </filter>  
    <filter type="log4net.Filter.DenyAllFilter" />  
    <encoding value="utf-8"/>  
    <preserveLogFileNameExtension value="true" />  
    <appendToFile value="true" />  
    <rollingStyle value="Date" />  
    <file value="Logs\\" />  
    <StaticLogFileName value="false"/>  
    <datePattern value="yyyyMMdd'-err.log'" />  
    <layout type="log4net.Layout.PatternLayout">  
      <conversionPattern value="%date - %message%newline" />  
    </layout>  
  </appender>  
  <root>  
    <level value="ALL" />  
    <appender-ref ref="infoAppender" />  
    <appender-ref ref="errorAppender" />  
  </root>  
  <logger name="Performance" additivity="false">  
    <level value="ALL" />  
  </logger>  
</log4net>  
相关文章
.Net Core使用 Log4Net
.Net Core使用 Log4Net
102 0
|
监控 C#
VS2012 C#使用/配置Log4Net
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨如何在VS2012中使用Log4Net 1、首先在项目中添加Nuget程序包.
1571 0
|
SQL 数据库 数据库管理
|
数据库管理 关系型数据库 数据格式
|
监控 关系型数据库 Java
|
数据库 Windows 数据库管理