• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netmvc中用log4net记录日志到数据库中

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET获取服务器变量信息发布时间:2022-07-10
下一篇:
ASP.NET常见错误,原因及解决方法(2003版)_不断更新.....发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap