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