使用log4net记录日志到数据库(含有自定义属性)
记录日志是管理系统中对用户行为的一种监控与审核,asp.net中记录日志的方式有很多种,这里我只介绍一下最近用到的log4net,关于他的具体介绍网上有很多,我讲一下他的用法。 第一步:在配置文件中的<configSections>节添加下面一句话 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 第二步:在<configuration>节中添加如下内容 < log4net>
<root >
<level value="Debug"/>
<appender-ref ref="ADONetAppender"/>
</root>
<logger name="myLogger">
<level value="Debug"/>
<appender-ref ref="ADONetAppender"/>
</logger> //关于上边root到logger这块,如果同时出现,有可能会出现重复插入记录的情况,那么就需要改一下代码,把上面两段代码改成如下一段代码,如下: <root >
<level value="Debug" name="myLogger"/>
<appender-ref ref="ADONetAppender"/>
</root> //下面就是对插入到数据库一些基本设置和基本字段设置
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender,log4net">
<!--BufferSize为缓冲区大小,只有日志记录超10条才会一块写入到数据库-->
<bufferSize value=""/>
<!--或写为<param name="BufferSize" value="" />-->
<!--引用-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<!--连接数据库字符串-->
<connectionString value="Data Source=.;Initial Catalog=audit;Persist Security Info=True;User ID=sa;Password=123;MultipleActiveResultSets=True"/>
<!--插入到表Log--> <commandText value="INSERT INTO T_AUDITINFO ([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"/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{Event_Type}"/>//注意这里,当用到property时,就表明这是用户自定义的字段属性啦,是log4net中所没有提供的字段。其中MyLayout是自定义属性所在的类,
LogComponent是类所在的命名空间,这是我们自己要写的部分,将在下面介绍。 </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=""/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{EventCategory}"/>
</layout>
</parameter>
<!--日志分类号-->
<parameter>
<parameterName value="@Event_ID"/>
<dbType value="Int32"/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{Event_ID}"/>
</layout>
</parameter>
<!--计算机IP-->
<parameter>
<parameterName value="@ComputerName"/>
<dbType value="String"/>
<size value=""/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{ComputerName}"/>
</layout>
</parameter>
<!--计算机Mac信息-->
<parameter>
<parameterName value="@Mac_Address"/>
<dbType value="String"/>
<size value=""/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{Mac_Address}"/>
</layout>
</parameter>
<!--登陆系统用户名-->
<parameter>
<parameterName value="@UserName"/>
<dbType value="String"/>
<size value=""/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{UserName}"/>
</layout>
</parameter>
<!--事件来源类型,这里默认为Rier-->
<parameter>
<parameterName value="@SourceType"/>
<dbType value="String"/>
<size value=""/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{SourceType}"/>
</layout>
</parameter>
<!--事件来源-->
<parameter>
<parameterName value="@Source"/>
<dbType value="String"/>
<size value=""/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{Source}"/>
</layout>
</parameter>
<!--事件描述-->
<parameter>
<parameterName value="@Description"/>
<dbType value="String"/>
<size value=""/>
<layout type="LogComponent.MyLayout, LogComponent">
<param name="ConversionPattern" value="%property{Description}"/>
</layout>
</parameter>
<!--日志收集时间--> <parameter>
<parameterName value="@CollectDate"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
</appender>
</log4net> 第三步:自定义类,这些类呢包含将要插入数据库中的自定义字段 命名空间为 LogComponent 包含3个类:LogContent.cs、 MyLayout.cs 、MyMessagePatternConverter .cs 第一个类 LogContent.cs 包含了所有的自定字段属性 using System;
using System.Data;
using System.Configuration;
using System.Web; /// <summary>
/// LogContent 的摘要说明
/// </summary>
public class LogContent
{
public LogContent(int eventType,string eventCategory,int eventID,string computerName,string macAddress,string userName,string sourceType,string source,string description)
{
_event_Type = eventType;
_eventCategory = eventCategory;
_event_ID = eventID;
_computerName = computerName;
_mac_Address = macAddress;
_userName = userName;
_sourceType = sourceType;
_source = source;
_description = description;
} int _event_Type;
/// <summary>
/// 时间类型 均为3
/// </summary>
public int Event_Type
{
get { return _event_Type; }
set { _event_Type = value; }
}
string _eventCategory;
/// <summary>
/// 日志分类描述,自定义
/// </summary>
public string EventCategory
{
get { return _eventCategory; }
set { _eventCategory = value; }
}
int _event_ID;
/// <summary>
/// 日志分类号
/// </summary>
public int Event_ID
{
get { return _event_ID; }
set { _event_ID = value; }
}
string _computerName;
/// <summary>
/// 计算机IP
/// </summary>
public string ComputerName
{
get { return _computerName; }
set { _computerName = value; }
}
string _mac_Address;
/// <summary>
/// 计算机Mac地址
/// </summary>
public string Mac_Address
{
get { return _mac_Address; }
set { _mac_Address = value; }
}
string _userName;
/// <summary>
/// 系统登陆用户
/// </summary>
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
string _sourceType;
/// <summary>
/// Rier
/// </summary>
public string SourceType
{
get { return _sourceType; }
set { _sourceType = value; }
}
string _source;
/// <summary>
/// Rier Recorder audit
/// </summary>
public string Source
{
get { return _source; }
set { _source = value; }
}
string _description;
/// <summary>
/// 日志描述信息
/// </summary>
public string Description
{
get { return _description; }
set { _description = value; }
} } 第二个类 MyLayout.cs 把我们定义的属性转换为log4net所能识别的属性 using System;
using System.Collections.Generic;
using System.Text;
using log4net.Layout.Pattern;
using log4net.Layout;
using log4net.Core;
using System.Reflection; namespace LogComponent
{
class MyLayout:PatternLayout
{
public MyLayout()
{
this.AddConverter("property", typeof(MyMessagePatternConverter));
}
}
} 第三个类
using System;
using System.Collections.Generic;
using System.Text;
using log4net.Layout.Pattern;
using log4net.Layout;
using log4net.Core;
using System.Reflection;
namespace LogComponent
{
class MyMessagePatternConverter:PatternLayoutConverter
{
protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
{
if (Option != null)
{
// Write the value for the specified key
WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));
}
else
{
// Write all the key value pairs
WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
}
//if (Option != null)
//{
// // Write the value for the specified key
// WriteObject(writer, loggingEvent.Repository, loggingEvent.LookupProperty(Option));
//}
//else
//{
// // Write all the key value pairs
// WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
//}
} /// <summary>
/// 通过反射获取传入的日志对象的某个属性的值
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
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); return propertyValue;
} }
} 代码页
记得在该项目中添加log4net引用 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LogComponent;
[assembly: log4net.Config.XmlConfigurator(Watch = true)] public partial class Test : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
log4net.ILog log = log4net.LogManager.GetLogger("myLogger"); log.Info(new LogContent(,"登陆系统",,"127.0.0.1","","mhy","","","登陆成功")); }
}
使用log4net记录日志到数据库(含有自定义属性)的更多相关文章
- 将WebService部署到 SharePoint 2010 gac 缓存中,并用Log4Net记录日志到数据库
最近做了一个sharePoint项目,需要实现的功能是,第三方网站访问我们sharePoint中的数据,通过Webservice方式实现文件的上传和下载. 于是代码工作完成了之后,本地调试没什么问题, ...
- Log4Net使用指南之用log4net记录日志到数据库(含有自定义属性)------附Demo例子源代码
Log4NET简介 log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具. 前提 最近做项目 ...
- 使用log4net记录日志到数据库(含自定义属性)
日志输出自定义属性! 特来总结一下: 一.配置文件 使用log4写入数据库就不多说了,网上方法很多,自定义字段如下 <commandText value="INSERT INTO db ...
- log4net记录日志到数据库自定义字段
假设数据库中有如下自定义字段: 1.根据自定义字段定义日志信息对象 public class MessageLog { /// <summary> ...
- asp.net mvc中用 log4net记录日志到数据库中
1.log4net官网配置相关,创建数据库 http://logging.apache.org/log4net/release/config-examples.html CREATE TABLE [d ...
- 如何配置Log4Net使用Oracle数据库记录日志
最近在做一个项目的时候,需要增加一个日志的功能,需要使用Log4Net记录日志,把数据插入到Oracle数据库,经过好久的研究终于成功了.把方法记录下来,以备以后查询. 直接写实现方法,分两步完成: ...
- Log4Net(三)之记录日志到数据库
前面两篇短文向大家介绍了如何使用log4net,以及如何将log4net记录到文本文件中.下面本文将向大家介绍如何将log4net记录到数据库中. 经过前面的介绍,我想大家对使用log4net的过程已 ...
- [转]如何配置Log4Net使用Oracle数据库记录日志
本文转自:http://www.cnblogs.com/PatrickLiu/p/6012153.html 最近在做一个项目的时候,需要增加一个日志的功能,需要使用Log4Net记录日志,把数据插入到 ...
- C# 使用 log4net 记录日志
Ø 前言 在一般的开发应用中,都会涉及到日志记录,用于排查错误 或 记录程序运行时的日志信息.log4net 库是 Apache log4j 框架在 Microsoft .NET 平台的实现,是一个 ...
随机推荐
- Entity Framework管理实体关系(二):管理一对二关系
在上一篇文章中,简单的介绍了使用Fluent API如何管理一对一的实体关系,在这篇文章中,接着介绍Fluent API如何管理一对多的实体关系. 要在数据库中配置一对多关系,我们可以依赖EF约定,还 ...
- SQL Server查询某个字段存在哪些表中
一.查询SQL Server中所有的表 SQL语句:SELECT * FROM sys.tables name列表示所有的表名. 二.查询SQL Server中所有的列 SQL语句:SELECT * ...
- windows rails new demo时候出错Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.
rails new demo --database=mysql最后报错Gem files will remain installed in D:/BillFiles/rails_dev/Ruby193 ...
- Eclipse 中link一个异地的Folder
Eclipse 中link一个外地的Folder New -> Folder -> Click "Advanced" --> Check "Link t ...
- jQuery源码分析-jQuery中的循环技巧
作者:nuysoft/JS攻城师/高云 QQ:47214707 EMail:nuysoft@gmail.com 声明:本文为原创文章,如需转载,请注明来源并保留原文链接. 前记:本文收集了jQuery ...
- 关于Cocos2d-x中父子节点的互动
1.子节点可以通过this->getParent()来获得相应的父节点,并且进行强制类型转换. ((Scene*)this->getParent())->getPhysicsWorl ...
- HGNC 数据库-人类基因组数据库
HGNC 全称为HUGO Gene Nomenclature Committee, 叫做 HUGO基因命名委员会,负责对人类基因组上包括蛋白编码基因, ncRNA基因,甲基因和其他基因在内的所有基因提 ...
- this总结
this总结,mark一下: Object中的this: Object方法中的this,指向的就是该对象,即谁调用this就指向谁,与C#等服务器语言的思想比较一致. let demo = { nam ...
- Ubuntu 安装 Kubernetes
Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-Pa ...
- Java 应用程序设计规范
1.能在程序中取的产生就从程序中取.不用客户输入(减少客户输入). 比如客户号 信息 等. 2.如果有参数输入尽可能减少参数输入的个数(4个->0个): 3.验证入参(尽可能的实现输入参数的正确 ...