log 4 netを使用してデータベースにログを記録する(カスタムプロパティを含む)

35784 ワード

                       ,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="10"/>
   <!--   <param name="BufferSize" value="10" />-->
   <!--  -->
   <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="50"/>
    <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="50"/>
    <layout type="LogComponent.MyLayout, LogComponent">
     <param name="ConversionPattern" value="%property{ComputerName}"/>
    </layout>
   </parameter>
   <!--   Mac  -->
   <parameter>
    <parameterName value="@Mac_Address"/>
    <dbType value="String"/>
    <size value="50"/>
    <layout type="LogComponent.MyLayout, LogComponent">
     <param name="ConversionPattern" value="%property{Mac_Address}"/>
    </layout>
   </parameter>
   <!--       -->
   <parameter>
    <parameterName value="@UserName"/>
    <dbType value="String"/>
    <size value="50"/>
    <layout type="LogComponent.MyLayout, LogComponent">
     <param name="ConversionPattern" value="%property{UserName}"/>
    </layout>
   </parameter>
   <!--      ,     Rier-->
   <parameter>
    <parameterName value="@SourceType"/>
    <dbType value="String"/>
    <size value="20"/>
    <layout type="LogComponent.MyLayout, LogComponent">
     <param name="ConversionPattern" value="%property{SourceType}"/>
    </layout>
   </parameter>
  <!--    -->
   <parameter>
    <parameterName value="@Source"/>
    <dbType value="String"/>
    <size value="50"/>
    <layout type="LogComponent.MyLayout, LogComponent">
     <param name="ConversionPattern" value="%property{Source}"/>
    </layout>
   </parameter>
   <!--    -->
   <parameter>
    <parameterName value="@Description"/>
    <dbType value="String"/>
    <size value="500"/>
    <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(3,"    ",1,"127.0.0.1","111111","mhy","11111","11111111","    "));
  

    }
}