码迷,mamicode.com
首页 > Web开发 > 详细

Log4net配置

时间:2015-05-14 18:22:37      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

一  AssemblyInfo.cs文件

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
二  创建log4net.config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section  name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>

    <!--写入到数据库-->
    <appender name="ADONetAppender_DbServer" type="log4net.Appender.AdoNetAppender">
      <!--错误队列数据达到5个才持久化到数据库-->
      <bufferSize value="1" />
      <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
      <param name="ConnectionString" value="Data Source=ip ;initial catalog=数据库;user id=**;password=**;pooling=true;max pool size=100"/>
      <commandText value="INSERT INTO log(Date,Thread,Level,Logger,Message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/>
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>

        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date{yyyy‘-‘MM‘-‘dd HH‘:‘mm‘:‘ss‘.‘fff}" />
        </layout>
        <!-- <layout type="log4net.Layout.RawTimeStampLayout"/>-->
      </parameter>
      <parameter>
        <parameterName value="@thread"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message"/>
        <dbType value="String"/>
        <size value="4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message"/>
        </layout>
      </parameter>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="ADONetAppender_DbServer"/>

    </root>
    <!-- Specify the level for some specific categories -->
    <logger name="iNotes">
      <!-- <appender-ref ref="B" /> -->
      <level value="ALL"/>
      <appender-ref ref="ADONetAppender_DbServer"/>

    </logger>


  </log4net>
</configuration>
三  创建Log.cs 文件

    public class Log
    {
        //获取实例
        private static log4net.ILog myLogger = log4net.LogManager.GetLogger(Log4NetConfig.DBServer);

        static Log()
        {
        }

        //错误级别:Info
        public static void Info(Exception ex, string message)
        {
            myLogger.Info(AppendMessage(ex, message));
        }
        //错误级别:Debug
        public static void Debug(Exception ex, string message)
        {
            myLogger.Debug(AppendMessage(ex, message));
        }
        //错误级别:Warn
        public static void Warn(Exception ex, string message)
        {
            myLogger.Warn(AppendMessage(ex, message));
        }
        //错误级别:Fatal
        public static void Fatal(Exception ex, string message)
        {
            myLogger.Fatal(AppendMessage(ex, message));
        }
        //错误级别:Error
        public static void Error(Exception ex, string message)
        {
            myLogger.Error(AppendMessage(ex, message));
        }
        //拼接错误信息
        private static string AppendMessage(Exception ex, string message)
        {

            return "自定义错误信息:" + message + "。系统错误信息:" + ex.Message + ",发生时间:" +
                   DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
四 创建Log4NetConfig.cs文件

public class Log4NetConfig
    {
        public static string DBServer = "ADONetAppender_DbServer";
        public static string File = "FileAppender";
    }
五,修改Global.asax文件

添加 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
六,根据log4net.config里的数据来创建数据库

七,必须添加的类库  log4net.dll,MySql.Data.dll,并且引用

八,必须把 log4net.dll,MySql.Data.dll复制一份放到bin文件夹下,否则不会写日志
九 ,本文来源于:http://www.cnblogs.com/gaobing/p/3809526.html

 

Log4net配置

标签:

原文地址:http://www.cnblogs.com/xiaoyaodijun/p/4503739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!