非HTTP协议应用程序的Elmah或无httpcontext的Elmah
-
04-10-2019 - |
题
我们正在研究3层应用程序,并且被允许使用最新,最大的(MVC2,IIS7.5,WCF,SQL2K8等)。该应用程序层通过WCF服务暴露于各种Web应用程序。由于我们控制了服务和客户端,因此我们决定使用net.tcp绑定来获得与HTTP相比的性能优势。
我们想使用 Elmah 对于网络应用程序和服务上的错误记录。这是我的问题。有很多有关将Elmah与WCF一起使用的信息,但这全都用于HTTP绑定。有谁知道/如何使用Elmah与WCF服务展示非HTTP端点?
我的猜测是不,因为Elmah想要HttpContext,这需要 AspnetCompatibalyEnabled 标志在web.config中为真。从 MSDN:
IIS 7.0,允许WCF服务通过HTTP以外的协议进行通信。但是,在已启用ASP.NET兼容模式的应用程序中运行的WCF服务不允许公开非HTTP端点。当服务收到其第一条消息时,这种配置会生成激活异常。
如果您不能将Elmah与具有非HTTP端点的WCF服务一起使用,那么后续问题是:我们可以以不需要HTTPContext的方式使用Elmah吗?或更普遍(以免提交 薄金属标尺 错误),是否有任何方法可以将Elmah与具有非HTTP端点的WCF服务使用?
笔记: 我知道我们可以下载Elmah源代码并将其更改以添加垫片或删除httpcontext依赖关系,但是我正在尝试避免分配代码。
解决方案
不,Elmah是HTTP模块,除非您服务于HTTP请求,否则Elmah不会做任何事情
其他提示
可能如下
Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));
参考:http://groups.google.com/group/elmah/browse_thread/thread/9ea4b51420fd5dfa
我早些时候尝试过 这 WCF服务的解决方案除了ASPNETCompatibility模式外,它没有在IIS托管的WCF服务上使用,但它在Visual Studio中托管了Dev Server托管的WCF服务。因此,我必须通过上述解决方案满足自己。
您是否尝试使用静态void appinitialize(){}方法来初始化它?初始化与WCF相关的事物时,它可以与非HTTP端点一起使用。
有关更多信息,请参见Wenlong Dong的出色博客文章:http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx
hth,
- 最终
我们将企业库异常处理块与Elmah结合使用来进行日常异常。而不是使用任何HTTP模块,它使用了直接调用日志到Elmah的任何HTTP模块。
public class ErrorHandlerServiceBehaviour : BehaviorExtensionElement, IServiceBehavior
{
#region BehaviourExtensionElement Members
public override Type BehaviorType
{
get { return typeof(ErrorHandlerServiceBehaviour); }
}
protected override object CreateBehavior()
{
return new ErrorHandlerServiceBehaviour();
}
#endregion
#region IServiceBehavior Members
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
{
channelDispatcher.ErrorHandlers.Add(new ElmahExceptionHandler());
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
#endregion
}
然后exceptionhandler
public class ElmahExceptionHandler : IErrorHandler
{
#region IErrorHandler Members
public bool HandleError(Exception error)
{
return ExceptionPolicy.HandleException(error, "ServiceExceptions");
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
}
#endregion
}
然后在app.config的企业库例外策略部分
<exceptionHandling>
<exceptionPolicies>
<add name="ServiceExceptions">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="Exception">
<exceptionHandlers>
<add type="DB.Framework.Logging.ElmahExceptionHandler, DinguBlue.Framework.Logging"
name="Elmah Exception Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
例如,请参见 http://dotnetslackers.com/articles/aspnet/getting-elmah-to-work-with-wcf-services.aspx