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

  • 相关阅读:
    codechef FNCS
    bzoj2653 middle
    CF698F Coprime Permutation
    CF538H Summer Dichotomy
    CF930E Coins Exhibition
    CF468D Tree
    CF528E Triangles3000
    BZOJ 4066: 简单题
    BZOJ 4300: 绝世好题
    BZOJ 4520: [Cqoi2016]K远点对
  • 原文地址:https://www.cnblogs.com/zhian/p/9669559.html
Copyright © 2011-2022 走看看