首页 > 代码库 > asp.net mvc使用log4gNetz

asp.net mvc使用log4gNetz

1. 下载安装log4gNet

2. 将 \bin\net\4.0\release\log4net.dll 复制到你的项目中 。

3. 将log4net.dll 添加引用到你的项目中。

4. 添加如下内容到 assemblyinfo.cs。

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

5.在项目中创建一个新的log4net的配置文件Log4Net.config。  
技术分享
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <!--数据日志-->
    <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value=http://www.mamicode.com/"D:\wwwroot\admin_citssh_com\logfiles\Info\" />
      <param name="AppendToFile" value=http://www.mamicode.com/"true" />
      <param name="rollingStyle" value=http://www.mamicode.com/"Date" />
      <param name="datePattern" value=http://www.mamicode.com/"yyyy-MM-dd.‘Info.log‘" />
      <param name="staticLogFileName" value=http://www.mamicode.com/"false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.mamicode.com/"记录时间:%date %n日志级别:  %-5level %n类型:%logger  %n描述:%message %newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value=http://www.mamicode.com/"INFO" />
        <param name="LevelMax" value=http://www.mamicode.com/"INFO" />
      </filter>
    </appender>
    <!--错误日志-->
    <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value=http://www.mamicode.com/"D:\wwwroot\admin_citssh_com\logfiles\Error\" />
      <param name="AppendToFile" value=http://www.mamicode.com/"true" />
      <param name="rollingStyle" value=http://www.mamicode.com/"Date" />
      <param name="datePattern" value=http://www.mamicode.com/"yyyy-MM-dd.‘error.log‘" />
      <param name="staticLogFileName" value=http://www.mamicode.com/"false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.mamicode.com/"记录时间:%date %n日志级别:  %-5level %n类型:%logger  %n描述:%message %newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value=http://www.mamicode.com/"ERROR" />
        <param name="LevelMax" value=http://www.mamicode.com/"ERROR" />
      </filter>
    </appender>
    <!--调试日志-->
    <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="File" value=http://www.mamicode.com/"D:\wwwroot\admin_citssh_com\logfiles\Debug\" />
      <param name="AppendToFile" value=http://www.mamicode.com/"true" />
      <param name="rollingStyle" value=http://www.mamicode.com/"Date" />
      <param name="datePattern" value=http://www.mamicode.com/"yyyy-MM-dd.‘debug.log‘" />
      <param name="staticLogFileName" value=http://www.mamicode.com/"false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value=http://www.mamicode.com/"记录时间:%date %n日志级别:  %-5level %n类型:%logger  %n描述:%message %newline%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value=http://www.mamicode.com/"DEBUG" />
        <param name="LevelMax" value=http://www.mamicode.com/"DEBUG" />
      </filter>
    </appender>
    <root>
      <level value=http://www.mamicode.com/"DEBUG" />
      <!--文件形式记录日志-->
      <appender-ref ref="ErrorRollingFileAppender" />
      <appender-ref ref="DebugRollingFileAppender" />
      <appender-ref ref="InfoRollingFileAppender" />
    </root>
  </log4net>
</configuration>
View Code
6.在全局配置中添加如下代码:

log4net.Config.XmlConfigurator.Configure();

7.添加log操作类:
技术分享
    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()
        {
            StackFrame frame;
            string str;
            string str1;
            bool flag;
            try
            {
                int num = 2;
                StackTrace stackTrace = new StackTrace();
                int length = stackTrace.GetFrames().Length;
                do
                {
                    int num1 = num;
                    num = num1 + 1;
                    frame = stackTrace.GetFrame(num1);
                    str = frame.GetMethod().DeclaringType.ToString();
                    flag = (!str.EndsWith("Exception") ? false : num < length);
                }
                while (flag);
                string name = frame.GetMethod().Name;
                str1 = string.Concat(str, ".", name);
            }
            catch
            {
                str1 = null;
            }
            return str1;
        }

        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);
        }
    }
View Code

 

asp.net mvc使用log4gNetz