Log4Net的使用

在项目中打开NuGet管理器,搜索Log4Net

 

在Golbal.asax里面添加 Log4Net注册信息

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

在web项目下的web.config里面添加 <log4net></log4net>节点,来配置Debug,Error, Info对应的信息格式和路径

<log4net>
    <!--数据日志-->
    <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value="Log\Info\" />
      <param name="AppendToFile" value="true" />
      <param name="rollingStyle" value="Date" />
      <param name="datePattern" value="yyyy-MM-dd.'Info.log'" />
      <param name="staticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%n%n%n 记录时间:%date %n日志级别:  %-5level %n出错类:%logger  %n错误描述:%message %newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="INFO" />
      </filter>
    </appender>
    <!--错误日志-->
    <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value="Log\Error\" />
      <param name="AppendToFile" value="true" />
      <param name="rollingStyle" value="Date" />
      <param name="datePattern" value="yyyy-MM-dd.'error.log'" />
      <param name="staticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%n%n%n 记录时间:%date %n日志级别:  %-5level %n出错类:%logger  %n错误描述:%message %newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR" />
        <param name="LevelMax" value="ERROR" />
      </filter>
    </appender>
    <!--调试日志-->
    <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value="Log\Debug\" />
      <param name="AppendToFile" value="true" />
      <param name="rollingStyle" value="Date" />
      <param name="datePattern" value="yyyy-MM-dd.'debug.log'" />
      <param name="staticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%n%n%n 记录时间:%date %n日志级别:  %-5level %n出错类:%logger  %n错误描述:%message %newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="DEBUG" />
      </filter>
    </appender>
    <root>
      <level value="DEBUG" />
      <!--文件形式记录日志-->
      <appender-ref ref="ErrorRollingFileAppender" />
      <appender-ref ref="DebugRollingFileAppender" />
      <appender-ref ref="InfoRollingFileAppender" />
    </root>
  </log4net>

  

 在项目中定义一个Static的Log类,这样便可以通过直接调用Log.Debug这样的方式直接使用Log4Net存储对应信息

public static class Log
    {
        public static void Debug(object message)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message);
        }

        public static void Debug(object message, Exception ex)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex);
        }

        public static void Error(object message)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message);
        }

        public static void Error(object message, Exception exception)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception);
        }

        private static string GetCurrentMethodFullName()
        {
            try
            {
                StackFrame frame;
                string str2;
                int num = 2;
                StackTrace trace = new StackTrace();
                int length = trace.GetFrames().Length;
                do
                {
                    frame = trace.GetFrame(num++);
                    str2 = frame.GetMethod().DeclaringType.ToString();
                }
                while (str2.EndsWith("Exception") && (num < length));
                string name = frame.GetMethod().Name;
                return (str2 + "." + name);
            }
            catch
            {
                return null;
            }
        }

        public static void Info(object message)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message);
        }

        public static void Info(object message, Exception ex)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex);
        }

        public static void Warn(object message)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message);
        }

        public static void Warn(object message, Exception ex)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex);
        }
    }

 

调用的时候,只需要引用对应的命名空间

执行上面代码的时候,会在web.config里面配置的对应路径下生成对应的路径和文件

 

 

posted @ 2019-02-20 17:13  我不是你的狗  阅读(180)  评论(0编辑  收藏  举报