• 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

  • 相关阅读:
    自动同步日期dos命令 | DOS命令自动同步时间
    Mysql字符串截取,去掉时间,匹配日期等于今日
    HTML指定页面编码
    Mysql连接字符,字段函数concat()
    功能强大的截图工具snipaste
    当页面提交时,执行相关JS函数检查输入是否合法
    DOM和BOM
    JS内建對象(Math,Number,String,Date)
    JS数组基础01(数组的创建,push,pop,unshift,shift,concat,join,splice,slice,sort.reverse,indexOf,三种排序)
    总结01(对象引用的赋值与对象的复制,函数作为对象及回调递归,区分数组与对象)
  • 原文地址:https://www.cnblogs.com/zhian/p/9669559.html
Copyright © 2020-2023  润新知