Mar*_*rth 62 asp.net wcf logging elmah exception
我们使用优秀的ELMAH来处理ASP.NET 3.5 Web应用程序中的未处理异常.除了使用REST功能使用的WCF服务之外,这对于所有站点都非常有效.当应用程序代码未处理的操作方法中发生异常时,WCF会以各种方式处理它,具体取决于服务协定和配置设置.这意味着该异常不会最终触发ELMAH使用的ASP.NET HttpApplication.Error事件.我知道要处理的两个解决方案是:
第一个选项非常简单,但并不完全是DRY.第二个选项仅要求您在实现属性和ErrorHandler后使用自定义属性装饰每个服务.我是根据Will的工作完成的,但我想在发布代码之前验证这是正确的方法.
有没有更好的方法让我错过了?
在MSDN documenation为IErrorHandler说,的HandleError方法是做记录的地方,但ELMAH访问HttpContext.Current.ApplicationInstance,即使HttpContext.Current可用,在此方法中为null.在ProvideFault方法中调用Elmah是一种解决方法,因为ApplicationInstance已设置,但这与API文档中描述的意图不匹配.我在这里错过了什么吗?文档确实声明您不应该依赖于在操作线程上调用的HandleError方法,这可能是ApplicationInstance在此范围内为空的原因.
小智 88
我的博客文章(在OP中引用)中的解决方案基于我们用于在错误状态期间更改HTTP响应代码的现有解决方案.
因此,对我们来说,将异常传递给ELMAH是一个单行更改.如果有更好的解决方案,我也很想知道它.
对于后代/参考,以及潜在的改进 - 这是当前解决方案的代码.
using System;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;
using System.Net;
using System.Web;
using Elmah;
namespace YourApplication
{
/// <summary>
/// Your handler to actually tell ELMAH about the problem.
/// </summary>
public class HttpErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error != null ) // Notify ELMAH of the exception.
{
if (System.Web.HttpContext.Current == null)
return;
Elmah.ErrorSignal.FromCurrentContext().Raise(error);
}
}
}
/// <summary>
/// So we can decorate Services with the [ServiceErrorBehaviour(typeof(HttpErrorHandler))]
/// ...and errors reported to ELMAH
/// </summary>
public class ServiceErrorBehaviourAttribute : Attribute, IServiceBehavior
{
Type errorHandlerType;
public ServiceErrorBehaviourAttribute(Type errorHandlerType)
{
this.errorHandlerType = errorHandlerType;
}
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
}
public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandler;
errorHandler = (IErrorHandler)Activator.CreateInstance(errorHandlerType);
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
channelDispatcher.ErrorHandlers.Add(errorHandler);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用ServiceErrorBehaviour属性装饰您的WCF服务:
[ServiceContract(Namespace = "http://example.com/api/v1.0/")]
[ServiceErrorBehaviour(typeof(HttpErrorHandler))]
public class MyServiceService
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
在创建BehaviorExtensionElement时,甚至可以使用config激活行为:
public class ErrorBehaviorExtensionElement : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(ServiceErrorBehaviourAttribute); }
}
protected override object CreateBehavior()
{
return new ServiceErrorBehaviourAttribute(typeof(HttpErrorHandler));
}
}
Run Code Online (Sandbox Code Playgroud)
配置:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="elmah" type="Namespace.ErrorBehaviorExtensionElement, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<elmah />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
这样也可以将ELMAH与RIA服务结合使用!
| 归档时间: |
|
| 查看次数: |
19248 次 |
| 最近记录: |