代码之家  ›  专栏  ›  技术社区  ›  Josh

用于非HTTP协议应用程序的elmah或不带httpcontext的elmah

  •  5
  • Josh  · 技术社区  · 16 年前

    我们正在开发一个三层应用程序,并且我们被允许使用最新和最棒的(MVC2、IIS7.5、WCF、SQL2K8等)。应用层由WCF服务向各种Web应用程序公开。由于我们同时控制了服务和客户端,因此我们决定使用net.tcp绑定来提高它们相对于HTTP的性能优势。

    我们想用 ELMAH 对于错误日志记录,包括在Web应用程序和服务上。这是我的问题。有很多关于将elmah与wcf结合使用的信息,但都是针对HTTP绑定的。是否有人知道您是否可以/如何将elmah与公开非HTTP端点的WCF服务一起使用?

    我想不是,因为Elmah想要httpContext,它需要 AspNetCompatibilityEnabled 在web.config中标记为true。从 MSDN :

    IIS 7.0和WAS允许WCF服务通过HTTP以外的协议进行通信。但是,在启用了ASP.NET兼容模式的应用程序中运行的WCF服务不允许公开非HTTP终结点。当服务收到第一条消息时,这样的配置会生成一个激活异常。

    如果您不能将elmah与具有非HTTP端点的WCF服务一起使用是真的,那么后续的问题是:我们能否以不需要httpcontext的方式使用elmah? 或更一般地(以便不 thin metal ruler 错误),是否有任何方法可以将elmah与具有非HTTP端点的WCF服务一起使用?

    注: 我知道我们可以下载elmah源代码并将其更改为添加一个填充程序或删除httpcontext依赖项,但我试图避免代码分叉。

    4 回复  |  直到 15 年前
        1
  •  5
  •   Midhat    16 年前

    不。Elmah是一个HTTP模块,除非您提供HTTP请求,否则Elmah不会做任何事情。

        2
  •  2
  •   Community Mohan Dere    9 年前

    可能如下

    Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));
    

    参考文献: http://groups.google.com/group/elmah/browse_thread/thread/9ea4b51420fd5dfa

    早些时候我尝试过 this 除了AspNetCompatibility模式外,WCF服务的解决方案和它在IIS托管的WCF服务上不起作用,但它在Visual Studio中的Dev Server托管的WCF服务上起作用。因此,我必须满足于上述解决方案。

        3
  •  0
  •   larsw    16 年前

    您是否尝试使用静态void AppInitialize()方法初始化它?在初始化与WCF相关的东西时,它与非HTTP端点一起工作。

    有关更多信息,请参阅温龙洞的优秀博客: http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx

    HTH

    --拉尔夫

        4
  •  0
  •   Rob    15 年前

    我们使用企业库异常处理块和elmah来记录异常。它不使用任何HTTP模块,而是直接使用Elmah的调用日志。

     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
    
    }
    

    然后是异常处理程序

     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