zoukankan      html  css  js  c++  java
  • WCF全局捕获日志

    /// <summary>
    /// WCF服务端异常处理器
    /// </summary>
    public class WCF_ExceptionHandler : IErrorHandler
    {
    public bool HandleError(Exception error)
    {
    return true;
    }

    public void ProvideFault(Exception ex, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message msg)
    {

    //
    //在这里处理服务端的消息,将消息写入服务端的日志
    //
    string err = string.Format("调用WCF接口 '{0}' 出错", ex.TargetSite.Name) + ",详情: " + ex.Message;
    Logger.LOG_ERROR.Error(err + ",ex=" + ex);
    var newEx = new FaultException(err);

    MessageFault msgFault = newEx.CreateMessageFault();
    msg = Message.CreateMessage(version, msgFault, newEx.Action);
    }
    }

    /// <summary>
    /// WCF服务类的错误的特性
    /// </summary>
    public class WCF_ExceptionBehaviourAttribute : Attribute, IServiceBehavior
    {
    private readonly List<Type> _errorHandlerTypelist;

    /// <summary>
    /// 注入单个的错误事件
    /// </summary>
    /// <param name="errorHandlerType"></param>
    public WCF_ExceptionBehaviourAttribute(Type errorHandlerType)
    {
    _errorHandlerTypelist = new List<Type>() { errorHandlerType };
    }
    /// <summary>
    /// 注入多个的自定义错误
    /// </summary>
    /// <param name="errorHandlerTypelist"></param>
    public WCF_ExceptionBehaviourAttribute(List<Type> errorHandlerTypelist)
    {
    _errorHandlerTypelist = errorHandlerTypelist;
    }

    #region IServiceBehavior Members
    /// <summary>
    /// 验证服务主机和服务描述
    /// </summary>
    /// <param name="description"></param>
    /// <param name="serviceHostBase"></param>
    public void Validate(ServiceDescription description,
    ServiceHostBase serviceHostBase)
    {
    Logger.LOG_INFO.Info(description.Name + "开启WCF异常捕获监听");
    }

    /// <summary>
    /// 绑定参数
    /// </summary>
    /// <param name="description"></param>
    /// <param name="serviceHostBase"></param>
    /// <param name="endpoints"></param>
    /// <param name="parameters"></param>
    public void AddBindingParameters(ServiceDescription description,
    ServiceHostBase serviceHostBase,
    Collection<ServiceEndpoint> endpoints,
    BindingParameterCollection parameters)
    {
    }
    /// <summary>
    /// 应用调度-更改运行时属性值或插入自定义的功能
    /// </summary>
    /// <param name="description"></param>
    /// <param name="serviceHostBase"></param>
    public void ApplyDispatchBehavior(ServiceDescription description,
    ServiceHostBase serviceHostBase)
    {
    if (_errorHandlerTypelist != null)
    {
    foreach (var _errorHandlerType in _errorHandlerTypelist)
    {
    var handler =
    (IErrorHandler)Activator.CreateInstance(_errorHandlerType);

    foreach (ChannelDispatcherBase dispatcherBase in
    serviceHostBase.ChannelDispatchers)
    {
    var channelDispatcher = dispatcherBase as ChannelDispatcher;
    if (channelDispatcher != null)
    channelDispatcher.ErrorHandlers.Add(handler);
    }
    }

    }
    }

    #endregion
    }

    使用

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [WCF_ExceptionBehaviour(typeof(WCF_ExceptionHandler))]
    public class AccountModuleService : IAccountModuleService
    {

    }

     如果WCF多,添加到Base里面即可,不用每个wcf都加,有一个Base类添加一遍就可以了。

    引用地址:转至博客:J.Y

  • 相关阅读:
    Mysql之正则匹配
    定时任务之elastic-job概述
    清晰讲解LSB、MSB和大小端模式及网络字节序
    极光消息推送多环境配置
    基于TSUNG对MQTT进行压力测试-测试结果
    基于TSUNG对MQTT进行压力测试-基础概念温习
    阻塞式/非阻塞式与同步/异步的区别
    干货!Jenkins下配置findbugs、pmd及checkstyle实现代码自动检测
    jar包
    java环境变量及Eclipse自动编译问题
  • 原文地址:https://www.cnblogs.com/zhian/p/9669559.html
Copyright © 2011-2022 走看看