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

  • 相关阅读:
    记录一次无法很好解决的问题
    java与进制转换
    花了点时间写了下测试框架
    利用eclipse或者pycharm编写monkeyrunner脚本,cmd打开应用“转转”并截图保存到D盘
    Instrumentation
    关于学生机受控应用的问题总结
    忙里偷闲一天
    linux下python3的安装(已安装python2的情况下)
    ROS上利用usb_cam读取摄像头图像
    ch8 -- directMethod
  • 原文地址:https://www.cnblogs.com/zhian/p/9669559.html
Copyright © 2011-2022 走看看