Wcf配置log4net

1.引用log4net dll文件

2.创建log4net.config文件并配置文件信息

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- Author:GaoBingBing-->
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <!--写入到数据库-->
    <appender name="AdoNetAppender_MySql" type="log4net.Appender.AdoNetAppender">
      <!--错误队列数据达到5个才持久化到数据库-->
      <bufferSize value="5" />
      <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/>
      <param name="ConnectionString" value="server=localhost;database=specialdb;Uid=sa;Pwd=sa;old syntax=yes"/>
      <commandText value="INSERT INTO log_serviceslog(log_datetime,log_thread,log_level,log_logger,log_message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/>
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
        <!--<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss}" />-->
      </parameter>
      <parameter>
        <parameterName value="@thread"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message"/>
        <dbType value="String"/>
        <size value="4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message"/>

        </layout>
      </parameter>
      <!--<parameter>
        <parameterName value="@exception"/>
        <dbType value="String"/>
       <size value="2000"/>
         <layout type="log4net.Layout.ExceptionLayout"/>
       </parameter>-->
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="AdoNetAppender_MySql"/>
    </root>
    <logger name="*">
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender_MySql" />
    </logger>
  </log4net>
</configuration>

3.在项目根目录下新建CustomServiceHostFactory并继承ServiceHostFactory重写CreateServiceHost方法

   CustomServiceHost继承ServiceHost重写CustomServiceHost方法启动错误日志(需引用System.ServiceModel.Activation)

using System;
using System.ServiceModel.Activation;
using System.ServiceModel;

namespace topicWCFServices
{
    public class CustomServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(
           Type serviceType, Uri[] baseAddresses)
        {
            CustomServiceHost customServiceHost =
               new CustomServiceHost(serviceType, baseAddresses);
            return customServiceHost;
        }
    }
    public class CustomServiceHost : ServiceHost
    {
        public CustomServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
            log4net.Config.XmlConfigurator.Configure();
        }
        protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();
        }
    }
}

4.在AssemblyInfo.cs文件中加上

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

5.创建错误日志记录文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class TopicServiceLog
{
    //获取实例
    private static log4net.ILog myLogger = log4net.LogManager.GetLogger("AdoNetAppender_MySql");

    //错误级别:Info
    public static void Info(string message)
    {
        myLogger.Info(message);
    }
    //错误级别:Debug
    public static void Debug(string message)
    {

        myLogger.Debug(message);
    }
    //错误级别:Warn
    public static void Warn(string message)
    {

        myLogger.Warn(message);
    }
    //错误级别:Fatal
    public static void Fatal(string message)
    {

        myLogger.Fatal(message);
    }
    //错误级别:Error
    public static void Error(string message)
    {

        myLogger.Error(message);
    }
}

ok,结束,已测试可以使用

转载于:https://www.cnblogs.com/zhhying/p/4238750.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值