记录日志是管理系统中对用户行为的一种监控与审核,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记录日志到数据库(含有自定义属性)的更多相关文章

  1. 将WebService部署到 SharePoint 2010 gac 缓存中,并用Log4Net记录日志到数据库

    最近做了一个sharePoint项目,需要实现的功能是,第三方网站访问我们sharePoint中的数据,通过Webservice方式实现文件的上传和下载. 于是代码工作完成了之后,本地调试没什么问题, ...

  2. Log4Net使用指南之用log4net记录日志到数据库(含有自定义属性)------附Demo例子源代码

    Log4NET简介 log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具. 前提 最近做项目 ...

  3. 使用log4net记录日志到数据库(含自定义属性)

    日志输出自定义属性! 特来总结一下: 一.配置文件 使用log4写入数据库就不多说了,网上方法很多,自定义字段如下 <commandText value="INSERT INTO db ...

  4. log4net记录日志到数据库自定义字段

    假设数据库中有如下自定义字段:   1.根据自定义字段定义日志信息对象     public class MessageLog     {           /// <summary> ...

  5. asp.net mvc中用 log4net记录日志到数据库中

    1.log4net官网配置相关,创建数据库 http://logging.apache.org/log4net/release/config-examples.html CREATE TABLE [d ...

  6. 如何配置Log4Net使用Oracle数据库记录日志

    最近在做一个项目的时候,需要增加一个日志的功能,需要使用Log4Net记录日志,把数据插入到Oracle数据库,经过好久的研究终于成功了.把方法记录下来,以备以后查询. 直接写实现方法,分两步完成: ...

  7. Log4Net(三)之记录日志到数据库

    前面两篇短文向大家介绍了如何使用log4net,以及如何将log4net记录到文本文件中.下面本文将向大家介绍如何将log4net记录到数据库中. 经过前面的介绍,我想大家对使用log4net的过程已 ...

  8. [转]如何配置Log4Net使用Oracle数据库记录日志

    本文转自:http://www.cnblogs.com/PatrickLiu/p/6012153.html 最近在做一个项目的时候,需要增加一个日志的功能,需要使用Log4Net记录日志,把数据插入到 ...

  9. C# 使用 log4net 记录日志

    Ø  前言 在一般的开发应用中,都会涉及到日志记录,用于排查错误 或 记录程序运行时的日志信息.log4net 库是 Apache log4j 框架在 Microsoft .NET 平台的实现,是一个 ...

随机推荐

  1. 一个残酷的生鲜O2O之梦

    三个年轻人,毕业一年,上海浦东张江开了一家生态有机蔬菜店-稻香麦甜. 首先,这不是一个关于成功励志的故事,相反的,我们走向了悬崖.经营2个月,最终以签字转让结束了实体店. 准备阶段 我,首先辞职,那时 ...

  2. 《FPGA全程进阶---实战演练》第三章之PCB叠层

    1.双面板 在双层板设计layout时,最好不要不成梳状结构,因为这样构成的电路,回路面积较大,但是只要对较重要的信号加以地保护,布线完成之后将空的地方敷上地铜皮,并在多个过孔将两个地连接起来,可以弥 ...

  3. 关于Cocos2d-x中增加暂停按钮的步骤

    1.在GameScene.cpp的init方法中先定义一个里面放着可变换并在变换的时候会响应事件的MenuItem的Menu,这个Menu里面的可变换MenuItem又由两个小MenuItem组成,每 ...

  4. TensorFlow基础笔记(0) tensorflow的基本数据类型操作

    import numpy as np import tensorflow as tf #build a graph print("build a graph") #生产变量tens ...

  5. adb调试功能

    参考: http://www.cnblogs.com/meil/archive/2012/05/24/2516055.html http://www.biemmeitalia.net/blog/and ...

  6. vector push_back报错

    场景:定义了一个结构体,包含一个vector的成员变量,在给这个vTQ push_back数据的时候报错. typedef struct tag_TQInfo { int iTime; int iMa ...

  7. 图像处理之3d算法----2d转3d算法介绍

    http://www.3dov.cn/html/c/37/index.html http://news.ifeng.com/a/20151117/46275220_0.shtml 磁力矩阵 http: ...

  8. ftp命令行工具如何 连接 非标准21端口(其他端口)的ftp服务器

    windows: step1:ftp命令进入ftp交互环境 step2:ftp>open ip空格port 然后...

  9. 从源代码来理解ArrayList和LinkedList差别

    从源代码理解ArrayList和LinkedList差别 ArrayList ArrayList默认容量为10,实质是一个数组用于存放元素,size表示ArrayList所包括的元素个数. Array ...

  10. css图片宽高相等设置

    <div class="image-header"> <img src="demo.jpg"> </div> .image- ...