asp.net mvc中用 log4net记录日志到数据库中
生活随笔
收集整理的這篇文章主要介紹了
asp.net mvc中用 log4net记录日志到数据库中
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.log4net官網(wǎng)配置相關(guān),創(chuàng)建數(shù)據(jù)庫
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)配置項(xiàng)
<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("你配置在配置文件中的數(shù)據(jù)庫名稱");adoNetAppender.ActivateOptions(); //Refresh AdoNetAppenders Settings}}}}/// <summary>/// 記錄調(diào)試(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>/// 記錄調(diào)試(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>/// 記錄調(diào)試(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>/// 記錄調(diào)試(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);}}
?
實(shí)際運(yùn)用
?
catch (Exception ex){LogHelper.AddErrorLog("Login", this.GetType(), ex);}
?
轉(zhuǎn)載于:https://www.cnblogs.com/Tom-yi/p/7805063.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的asp.net mvc中用 log4net记录日志到数据库中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的reduce函数用法
- 下一篇: 15. 二进制中1的个数