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

  • 相关阅读:
    苹果p12文件--一个苹果证书怎么多次使用(蛋疼,这些问题只有和其他企业合作才会遇到,别人的账号不可能给你,蛋疼....)
    xcode 树形管理 cocos2d-x的资源
    cocos2d-x 扩充引擎基类功能 引起的头文件重复包含问题的分析
    pytest--运行指定的测试和参数化
    adb--常用命令
    appium--desktop
    adb--环境安装
    pytest--命令行常用参数
    django -- auth模块
    pytest--常用插件
  • 原文地址:https://www.cnblogs.com/zhian/p/9669559.html
Copyright © 2011-2022 走看看