效果图:
1:第一步创建SQL表结构
-
CREATE TABLE [dbo].[LogDetails] (
-
[LogID] int NOT NULL IDENTITY(1,1) ,
-
[LogDate] datetime NOT NULL ,
-
[LogThread] nvarchar(100) NOT NULL ,
-
[LogLevel] nvarchar(200) NOT NULL ,
-
[LogLogger] nvarchar(500) NOT NULL ,
-
[LogMessage] nvarchar(3000) NOT NULL ,
-
[LogActionClick] nvarchar(4000) NULL ,
-
[UserName] nvarchar(30) NULL ,
-
[UserIP] varchar(20) NULL
-
)
2:创建项目然后下载log4net.dll 在项目中添加引用 http://logging.apache.org/log4net/download_log4net.cgi下载Binaries 下面的
3:创建 log4net.config
<?xml version="1.0" encoding="utf-8" ?>
-
<log4net debug="false">
-
-
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
-
-
<bufferSize value="0" />
-
-
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
-
<connectionString value="DATABASE=GoodLook;SERVER=xx.xx.140.170;UID=sa;PWD=123456a;Connect Timeout=30;" />
-
-
<commandText value="INSERT INTO LogDetails ([LogDate],[LogThread],[LogLevel],[LogLogger],[LogActionClick],[LogMessage],[UserName],[UserIP]) VALUES (@log_date, @thread, @log_level, @logger, @ActionsClick, @message,@UserName,@UserIP)" />
-
-
-
<parameterName value="@log_date" />
-
<dbType value="DateTime" />
-
<layout type="log4net.Layout.RawTimeStampLayout" />
-
-
-
-
<parameterName value="@thread" />
-
<dbType value="String" />
-
-
<layout type="log4net.Layout.PatternLayout">
-
<conversionPattern value="%t" />
-
-
-
-
-
<parameterName value="@log_level" />
-
<dbType value="String" />
-
-
<layout type="log4net.Layout.PatternLayout">
-
<conversionPattern value="%p" />
-
-
-
-
-
<parameterName value="@logger" />
-
<dbType value="String" />
-
-
<layout type="log4net.Layout.PatternLayout">
-
<conversionPattern value="%logger" />
-
-
-
-
<parameterName value="@message" />
-
<dbType value="String" />
-
-
<layout type="Log4NetApply.MyLayout">
-
<conversionPattern value="%property{Message}" />
-
-
-
-
<parameterName value="@ActionsClick" />
-
<dbType value="String" />
-
-
<layout type="Log4NetApply.MyLayout" >
-
<conversionPattern value = "%property{ActionsClick}"/>
-
-
-
-
-
<parameterName value="@UserName" />
-
<dbType value="String" />
-
-
<layout type="Log4NetApply.MyLayout" >
-
-
-
<conversionPattern value = "%property{UserName}"/>
-
-
-
-
<parameterName value="@UserIP" />
-
<dbType value="String" />
-
-
<layout type="Log4NetApply.MyLayout" >
-
<conversionPattern value = "%property{UserIP}"/>
-
-
-
-
-
-
-
-
-
-
<appender-ref ref="ADONetAppender" />
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<appender name="ReflectionLayout" type="log4net.Appender.RollingFileAppender,log4net">
-
-
<param name="File" value="D:/Log/" />
-
-
<param name="AppendToFile" value="true" />
-
-
-
-
-
-
-
-
<param name="StaticLogFileName" value="false" />
-
-
<param name="RollingStyle" value="Composite" />
-
-
<param name="DatePattern" value="yyyy-MM-dd/"ReflectionLayout.log"" />
-
-
-
-
<param name="maximumFileSize" value="500KB" />
-
-
<layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog">
-
<param name="ConversionPattern" value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger 操作者ID:%property{Operator} 操作类型:%property{Action}%n 当前机器名:%property%n当前机器名及登录用户:%username %n 记录位置:%location%n 消息描述:%property{Message}%n 异常:%exception%n 消息:%message%newline%n%n" />
-
-
-
4:在Web.config 里面加 configSections 节点 (CS可直接写在app.config里面,或者都写在一个config里面)
<configuration>
<strong> <span style="color:#FF0000;"> <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net configSource="log4net.config"/></span></strong>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
5:Properties 属性下面的AssemblyInfo.cs 追加 必须有这个,否则写入不到数据库中
- //[assembly: log4net.Config.XmlConfigurator(Watch = true)]
- //注意: ConfigFile 可以指定相对路径 和 绝对路径。 eg: /log/xxxx.log 或者 d://log//xxxx.log
- [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
6:添加 Global.asax ,然后在Application_Start 追加 读取配置程序文件
-
protected void Application_Start(object sender, EventArgs e)
-
{
-
//应用程序启动时,自动加载配置log4Net
-
XmlConfigurator.Configure();
-
}
7:创建自定义类 LogPublicClass.cs,为了方便写入数据库中 自定义的一些信息
using log4net.Layout;
using log4net.Layout.Pattern;
-
-
using System.Collections.Generic;
-
-
-
-
-
-
-
-
-
-
-
public LogContent(string macAddress, string computerName, string actionsclick, string description)
-
-
-
-
ActionsClick = actionsclick;
-
-
-
-
-
-
public string UserIP { get; set; }
-
-
-
-
public string UserName { get; set; }
-
-
-
-
public string ActionsClick { get; set; }
-
-
-
-
public string Message { get; set; }
-
-
public class MyLayout : PatternLayout
-
-
-
-
this.AddConverter("property", typeof(LogInfoPatternConverter));
-
-
-
public class LogInfoPatternConverter : PatternLayoutConverter
-
-
protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
-
-
-
-
-
WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));
-
-
-
-
-
WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
-
-
-
-
-
-
-
-
private object LookupProperty(string property, log4net.Core.LoggingEvent loggingEvent)
-
-
object propertyValue = string.Empty;
-
PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property);
-
if (propertyInfo != null)
-
propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null);
-
-
-
-
8:示例使用
-
try
-
{
-
log.Info(new LogContent("127.0.0.1", "111111", "登陆系统", "登陆成功"));
-
var ss = 1 - int.Parse("sss");
-
}
-
catch(Exception ex)
-
{
-
log.Error(new LogContent("127.0.0.1", "111111", "登陆系统", ex.Message+":"+ex.StackTrace));
-
}
|
请发表评论