C# 利用log4net 把日志写入到数据库

  • Post author:
  • Post category:其他


效果图:


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">
  <!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
  <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
    <!--日志缓存写入条数 设置为0时只要有一条就立刻写到数据库-->
    <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)" />
    <!--日志时间LogDate -->
     <parameter>
      <parameterName value="@log_date" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <!--线程号-->
    <parameter>
      <parameterName value="@thread" />
      <dbType value="String" />
      <size value="100" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%t" />
      </layout>
    </parameter>
    <!--日志类型LogLevel -->
    <parameter>
      <parameterName value="@log_level" />
      <dbType value="String" />
      <size value="200" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%p" />
      </layout>
    </parameter>
    <!--日志名称-->
    <parameter>
      <parameterName value="@logger" />
      <dbType value="String" />
      <size value="500" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%logger" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="3000" />
      <layout type="Log4NetApply.MyLayout">
        <conversionPattern value="%property{Message}" />
      </layout>
    </parameter>
    <parameter>
      <parameterName value="@ActionsClick" />
      <dbType value="String" />
      <size value="4000" />
        <layout type="Log4NetApply.MyLayout" >
        <conversionPattern value = "%property{ActionsClick}"/>
      </layout>
    </parameter>
    <!--自定义UserName -->
    <parameter>  
        <parameterName value="@UserName" />  
        <dbType value="String" />  
        <size value="30" />
      <layout type="Log4NetApply.MyLayout" >
        <!--log4net.MDC.Set("UserName", "asdfasdf");
        <conversionPattern value = "%X{UserName}"/>-->
        <conversionPattern value = "%property{UserName}"/>
      </layout>
      </parameter>  
   <parameter>  
        <parameterName value="@UserIP" />  
        <dbType value="String" />  
        <size value="20" />
        <layout type="Log4NetApply.MyLayout" >
          <conversionPattern value = "%property{UserIP}"/>
        </layout>
      </parameter>  
  </appender>
  <!-- setup the root category, add the appenders and set the default level -->
  <root>
    <level value="Warn"/><!-- 定义记录的日志级别-->
    <level value="Info"/>
    <level value="Debug"/>
    <level value="Fine"/>
    <appender-ref ref="ADONetAppender" /><!-- 记录到什么介质中-->
  </root>
  <!-- specify the level for some specific categories -->
  <!--//关于上边root到logger这块,如果同时出现,有可能会出现重复插入记录的情况:-->
  <!--<logger name="iNotes">
    <level value="WARN"/>
    <level value="INFO"/>
    <level value="DEBUG"/>
    <level value="FINE"/>
    <appender-ref ref="ADONetAppender"/>
  </logger>
  <logger name="StellaLogger">
    <level value="ALL"/>
    <appender-ref ref="AdoNetAppender" />
  </logger>-->
  <appender name="ReflectionLayout" type="log4net.Appender.RollingFileAppender,log4net">

    <!--日志文件路径,按文件大小方式输出时在这里指定文件名,并且前面的日志按天在文件名后自动添加当天日期形成文件-->
    <param name="File" value="D:/Log/" />

    <!--是否追加到文件-->
    <param name="AppendToFile" value="true" />

    <!--记录日志写入文件时,不锁定文本文件-->
    <!--<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />-->

    <!--Unicode编码-->
    <!--<Encoding value="UTF-8" />-->

    <!--最多产生的日志文件数,value="-1"为不限文件数-->
    <!--<param name="MaxSizeRollBackups" value="10" />-->

    <!--是否只写到一个文件中-->
    <param name="StaticLogFileName" value="false" />

    <!--按照何种方式产生多个日志文件(日期[Date],文件大小[Size],混合[Composite])-->
    <param name="RollingStyle" value="Composite" />

    <!--按日期产生文件夹,文件名[在日期方式与混合方式下使用]-->
    <param name="DatePattern" value="yyyy-MM-dd/"ReflectionLayout.log""  />
    <!--<param name="DatePattern" value="yyyyMMdd/yyyyMMdd"-TimerServer.log""  />-->
    <!--<param name="DatePattern" value="yyyyMMdd/"TimerServer/TimerServer.log""  />-->

    <!--每个文件的大小。只在混合方式与文件大小方式下使用,超出大小的在文件名后自动增加1重新命名-->
    <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" />
    </layout>
  </appender>

  
 
  
</log4net>

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;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace Log4NetApply
{
    /// <summary>
    /// 包含了所有的自定字段属性
    /// </summary>
    public class LogContent
    {
        public LogContent(string macAddress, string computerName, string actionsclick, string description)
        {
            UserIP = macAddress;
            UserName = computerName;
            ActionsClick = actionsclick;
            Message = description;
        }
   
        /// <summary>
        /// 访问IP
        /// </summary>
        public string UserIP { get; set; }

        /// <summary>
        /// 系统登陆用户
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 动作事件
        /// </summary>
        public string ActionsClick { get; set; }

        /// <summary>
        /// 日志描述信息
        /// </summary>
        public string Message { get; set; }


    }
	public class MyLayout : PatternLayout
    {
         public MyLayout()
        {
            this.AddConverter("property", typeof(LogInfoPatternConverter));
         }
     }

    public class LogInfoPatternConverter : 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());
            }
        }
        /// <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;
        }
    }
}

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));
                }

其他自行参考下列文章

http://www.cnblogs.com/kissazi2/p/3393151.html
http://blog.csdn.net/zdw_wym/article/details/48802821
http://blog.csdn.net/ydm19891101/article/details/50561638
http://www.w2bc.com/Article/70140
http://www.cnblogs.com/yuangang/archive/2016/05/16/5497140.html



版权声明:本文为kongwei521原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。