Wcf配置log4net

1.引用log4net dll文件

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

"1.0" encoding="utf-8"?>

  
  
    
"log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> "AdoNetAppender_MySql" type="log4net.Appender.AdoNetAppender"> "5" /> "ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/> "ConnectionString" value="server=localhost;database=specialdb;Uid=sa;Pwd=sa;old syntax=yes"/> "INSERT INTO log_serviceslog(log_datetime,log_thread,log_level,log_logger,log_message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/> "@log_date"/> "DateTime"/> "log4net.Layout.RawTimeStampLayout"/> "@thread"/> "String"/> "255"/> "log4net.Layout.PatternLayout"> "%thread"/> "@log_level"/> "String"/> "50"/> "log4net.Layout.PatternLayout"> "%level"/> "@logger"/> "String"/> "255"/> "log4net.Layout.PatternLayout"> "%logger"/> "@message"/> "String"/> "4000"/> "log4net.Layout.PatternLayout"> "%message"/> "ALL"/> ref ref="AdoNetAppender_MySql"/> "*"> "ALL" /> ref ref="AdoNetAppender_MySql" />

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

你可能感兴趣的:(Wcf配置log4net)