1.log4net官网配置相关,创建数据库

http://logging.apache.org/log4net/release/config-examples.html

CREATE TABLE [dbo].[tbl_LogInfo](
[ID] [int] IDENTITY(1,1) NOT NULL,
[LogTime] [datetime] NOT NULL,
[Thread] [nvarchar](max) NOT NULL,
[LogLevel] [nvarchar](max) NULL,
[Logger] [nvarchar](max) NULL,
[Message] [nvarchar](max) NULL,
[Exception] [nvarchar](max) NULL)

  配置项

 <configuration>
<log4net>
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
      <bufferSize value="10"/>
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=2sdfg57345c5619dsfsdf"/>
      <commandText value="INSERT INTO tbl_LogInfo ([LogTime],[Thread],[LogLevel],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"/>
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <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>
      <parameter>
        <parameterName value="@exception"/>
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="log4net.Layout.ExceptionLayout"/>
      </parameter>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="LogFileAppender"/>
      <appender-ref ref="ADONetAppender"/>
    </root>
  </log4net>
</configuration>

  

2.在C#中使用

工具类

   public class LogHelper
    {
        private const string configFileName = "Web.config";
        public LogHelper()
        {

        }
        private static void SetXmlConfigurator()
        {
            XmlConfigurator.Configure(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + configFileName));
            ConfigureLog4Net();
        }
        private static void ConfigureLog4Net()
        {
            Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
            if (hierarchy != null && hierarchy.Configured)
            {
                foreach (IAppender appender in hierarchy.GetAppenders())
                {
                    if (appender is AdoNetAppender)
                    {
                        var adoNetAppender = (AdoNetAppender)appender;
                        adoNetAppender.ConnectionString =ConfigurationHelper.GetConnectionString("你配置在配置文件中的数据库名称");
                        adoNetAppender.ActivateOptions(); //Refresh AdoNetAppenders Settings
                    }
                }
            }
        }
        /// <summary>
        /// 记录调试(Debug)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        /// <param name="ex">记录的异常</param>
        public static void AddDebugLog(string message, Type type, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Debug(message, ex);
        }
        /// <summary>
        /// 记录调试(Debug)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        /// <param name="ex">记录的异常</param>
        public static void AddDebugLog(string message, string actionName, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Debug(message, ex);
        }
        /// <summary>
        /// 记录调试(Debug)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        public static void AddDebugLog(string message, string actionName)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Debug(message);
        }
        /// <summary>
        /// 记录调试(Debug)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        public static void AddDebugLog(string message, Type type)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Debug(message);
        }
        /// <summary>
        /// 记录错误(Error)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        /// <param name="ex">记录的异常</param>
        public static void AddErrorLog(string message, Type type, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Error(message, ex);
        }
        /// <summary>
        /// 记录错误(Error)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        /// <param name="ex">记录的异常</param>
        public static void AddErrorLog(string message, string actionName, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Error(message, ex);
        }
        /// <summary>
        /// 记录错误(Error)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        public static void AddErrorLog(string message, string actionName)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Error(message);
        }
        /// <summary>
        /// 记录错误(Error)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        public static void AddErrorLog(string message, Type type)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Error(message);
        }
        /// <summary>
        /// 记录致命错误(Fatal)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        /// <param name="ex">记录的异常</param>
        public static void AddFatalLog(string message, Type type, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Fatal(message, ex);
        }
        /// <summary>
        /// 记录致命错误(Fatal)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        /// <param name="ex">记录的异常</param>
        public static void AddFatalLog(string message, string actionName, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Fatal(message, ex);
        }
        /// <summary>
        /// 记录致命错误(Fatal)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        public static void AddFatalLog(string message, string actionName)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Fatal(message);
        }
        /// <summary>
        /// 记录致命错误(Fatal)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        public static void AddFatalLog(string message, Type type)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Fatal(message);
        }
        /// <summary>
        /// 记录警告(Warn)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        /// <param name="ex">记录的异常</param>
        public static void AddWarnLog(string message, Type type, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Warn(message, ex);
        }
        /// <summary>
        /// 记录警告(Warn)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        /// <param name="ex">记录的异常</param>
        public static void AddWarnLog(string message, string actionName, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Warn(message, ex);
        }
        /// <summary>
        /// 记录警告(Warn)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        public static void AddWarnLog(string message, string actionName)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Warn(message);
        }
        /// <summary>
        /// 记录警告(Warn)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        public static void AddWarnLog(string message, Type type)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Warn(message);
        }
        /// <summary>
        /// 记录普通信息(Info)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        /// <param name="ex">记录的异常</param>
        public static void AddInfoLog(string message, Type type, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Info(message, ex);
        }
        /// <summary>
        /// 记录普通信息(Info)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        /// <param name="ex">记录的异常</param>
        public static void AddInfoLog(string message, string actionName, Exception ex)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Info(message, ex);
        }
        /// <summary>
        /// 记录普通信息(Info)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="actionName">记录操作的名称</param>
        public static void AddInfoLog(string message, string actionName)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(actionName);
            logger.Info(message);
        }
        /// <summary>
        /// 记录普通信息(Info)日志
        /// </summary>
        /// <param name="message">记录的信息</param>
        /// <param name="type">记录操作的类</param>
        public static void AddInfoLog(string message, Type type)
        {
            SetXmlConfigurator();
            ILog logger = log4net.LogManager.GetLogger(type);
            logger.Info(message);
        }
    }

  

 

实际运用

 

 catch (Exception ex)
            {
                LogHelper.AddErrorLog("Login", this.GetType(), ex);
            }

  

 

posted on 2017-11-08 17:10  Hai_yi  阅读(1194)  评论(2编辑  收藏  举报