C#:MVC引用Log4Net生成错误日志

第一步:引用log4net配置文件

第二步:在自己项目下新建文件夹LogNet,再在里面建立类Log.cs

log.cs内容如下:

public class Log
    {
        public static void WriteFatal(Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Fatal("严重错误", ex);
        }

        /// <summary>
        /// 输出异常信息
        /// </summary>
        /// <param name="t"></param>
        /// <param name="e"></param>
        public static void WriteLog(Type t, Exception e)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Error("Error", e);
        }

        /// <summary>
        /// 输出普通错误信息
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteLog(Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Error("Error", ex);
        }

        /// <summary>
        /// 输出DEBUG信息
        /// </summary>
        /// <param name="text"></param>
        public static void WriteDebug(object text)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("Debug信息");
            log.Debug(text);
        }

        /// <summary>
        /// 输出程序运行信息
        /// </summary>
        /// <param name="text"></param>
        public static void WriteInfo(string text)
        {
            ILog log = LogManager.GetLogger("程序运行信息");
            log.Info(text);
        }
    }

第三步:在自己项目下新建Log4Net.config

Log4Net.config内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="./Logs/%date{yyyy-MM-dd}.log"></file>
      <appendToFile value="true" />
      <rollingStyle value="composite" />
      <maxSizeRollBackups value="20" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <maximumFileSize value="600KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level - %F - %L%n    %logger %m%n" />
      </layout>
      <!--ALL DEBUG INFO WARN ERROR FATAL OFF-->
      <!--
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="ERROR" />
      </filter>
      <filter type="log4net.Filter.LevelMatchFilter">
        <param name="LevelToMatch" value="ERROR" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
      -->
    </appender>
    <!--定义输出到控制台命令行中-->
    <appender name="ConsoleAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level - %message%n" />
      </layout>
    </appender>
    <root>
      <!--ALL DEBUG INFO WARN ERROR FATAL OFF-->
      <level value="ALL" />
      <!-- 文件形式记录日志 -->
      <appender-ref ref="LogFileAppender" />
      <!-- 控制台控制显示日志 -->
      <appender-ref ref="ConsoleAppender" />
    </root>
    <!--
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    -->
  </log4net>
</configuration>

 第四步:在项目中搜索Global.asax.cs中加载log4net配置文件

log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/log4net.config")));

第五步:也是比较重要的一步,就是在AssemblyInfo.cs中注册这个Log4Net.config,不然是不会自动生成text文件的。

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

第六步:然后就可以在控制器函数里面去引用Log。

 

posted @ 2019-07-10 09:35  进步者One  阅读(773)  评论(0编辑  收藏  举报