1. 新建一個配置文件log4net.config,配置方法同成web.config或app.config一致; <?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <section name="log4net ...
1. 新建一個配置文件log4net.config,配置方法同成web.config或app.config一致;
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net debug="true">
<logger name="myLogger">
<level value="INFO"/>
<appender-ref ref="AdoNetAppender_SqlServer"/>
</logger>
<!--sql server資料庫1-->
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
<!-- BufferSize 為緩衝區大小,只有日誌記錄超5 條才會一塊寫入到資料庫 -->
<!-- 或寫為<param name="BufferSize" value="10" /> -->
<bufferSize value="0"/>
<!-- 引用 -->
<!--2.0這是對應sql2008 如是2000或2005另外配置-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089"/>
<!-- 連接資料庫字元串 -->
<connectionString value="Data Source=.;Initial Catalog=Log;User ID=sa;Password=123456;" />
<!-- 插入到表Log -->
<commandText value = "INSERT INTO Mylogger ([EVENTTYPE],[TIMESTAMP],[EVENTCATEGORY],[EVENT_ID],[COMPUTERNAME],[MAC_ADDRESS],[USERNAME],[SOURCETYPE],[SOURCE],[DESCRIPTION],[COLLECTDATE]) VALUES (@Event_Type,@log_date, @EventCategory, @Event_ID, @ComputerName,@Mac_Address,@UserName,@SourceType,@Source,@Description,@CollectDate) "/>
<!-- 日誌類型,這裡均為3 -->
<parameter>
<parameterName value = "@Event_Type"/>
<dbType value = "Int32"/>
<!--<dbType value = "String"/>
<size value = "50"/>-->
<!-- LogComponent 是類所在的命名空間,MyLayout 是自定義屬性所在的類,這是我們自己要寫的部分,將在下麵介紹。 -->
<layout type = "SuperAuth.Infrastructure.MyLayout">
<!-- 當用到property 時,就表明這是用戶自定義的欄位屬性啦,是log4net 中所沒有提供的欄位。 -->
<conversionPattern value = "%property{Event_Type} "/>
</layout>
</parameter>
<!-- 日誌記錄時間,RawTimeStampLayout 為預設的時間輸出格式 -->
<parameter>
<parameterName value = "@log_date"/>
<dbType value = "DateTime"/>
<layout type = "log4net.Layout.RawTimeStampLayout"/>
<!-- /這裡呢是獲取log4net 中提供的日誌時間 -->
</parameter>
<!-- 日誌分類描述 -->
<parameter>
<parameterName value = "@EventCategory"/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{EventCategory}"/>
</layout>
</parameter>
<!-- 日誌分類號 -->
<parameter>
<parameterName value = "@Event_ID"/>
<dbType value = "Int32"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Event_ID}"/>
</layout>
</parameter>
<!-- 電腦IP -->
<parameter>
<parameterName value = "@ComputerName"/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{ComputerName}"/>
</layout>
</parameter>
<!-- 電腦Mac 信息 -->
<parameter>
<parameterName value = "@Mac_Address"/>
<dbType value = "String"/>
<size value = "50 "/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Mac_Address}"/>
</layout>
</parameter>
<!-- 登陸系統用戶名 -->
<parameter>
<parameterName value = "@UserName"/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{UserName}"/>
</layout>
</parameter>
<!-- 事件來源類型,這裡預設為Rier -->
<parameter>
<parameterName value = "@SourceType"/>
<dbType value = "String"/>
<size value = "20"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{SourceType}"/>
</layout>
</parameter>
<!-- 事件來源 -->
<parameter>
<parameterName value = "@Source "/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Source}"/>
</layout>
</parameter>
<!-- 事件描述 -->
<parameter>
<parameterName value = "@Description "/>
<dbType value = "String"/>
<size value = "4000"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Description}"/>
</layout>
</parameter>
<!-- 日誌收集時間 -->
<parameter>
<parameterName value = "@CollectDate"/>
<dbType value = "DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
</appender>
</log4net>
<system.web>
<compilation debug="true" targetFramework="4.6.2" />
<httpRuntime targetFramework="4.6.2" />
</system.web>
</configuration>
2.建立相關的類文件如下:
public class LogHelper
{
private LogHelper()
{
SetConfig();
}
private static readonly log4net.ILog loginfo =LogManager.GetLogger("myLogger");
private static bool IsLoadConfig = false;
private static void SetConfig()
{
XmlConfigurator.Configure();
}
/// <summary>
/// 記錄日誌
/// </summary>
/// <param name="info">提示信息</param>
public static void WriteLog(string info)
{
if (!IsLoadConfig)
{
SetConfig();
IsLoadConfig = true;
}
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
/// <summary>
/// 記錄日誌
/// </summary>
/// <param name="info">提示信息</param>
public static void WriteLog(object info)
{
if (!IsLoadConfig)
{
SetConfig();
IsLoadConfig = true;
}
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
/// <summary>
/// 記錄異常
/// </summary>
/// <param name="info">錯誤</param>
/// <param name="ex">Exception</param>
public static void WriteLog(string info, Exception ex)
{
if (!IsLoadConfig)
{
SetConfig();
IsLoadConfig = true;
}
if (loginfo.IsErrorEnabled)
{
loginfo.Error(info, ex);
}
}
3.在使用類的地方引入log4net.config如下
[assembly: XmlConfigurator(ConfigFile= @"Log4net.config", Watch = true)]
4.最重要的一步,也是很多人出錯的地方,右鍵log4net.config點擊屬性,按如下設置即可。