【问题标题】:ELMAH - Exception Logging without having HttpContextELMAH - 没有 HttpContext 的异常日志记录
【发布时间】:2011-03-01 03:18:15
【问题描述】:

我尝试使用 Elmah.XmlFileErrorLog 解决 this 解决方案,但出现以下异常

System.ArgumentNullException was unhandled by user code
  Message="Value cannot be null.\r\nParameter name: context"
  Source="Elmah"
  ParamName="context"
  StackTrace:
       at Elmah.ErrorSignal.FromContext(HttpContext context) in c:\builds\ELMAH\src\Elmah\ErrorSignal.cs:line 67
       at Elmah.ErrorSignal.FromCurrentContext() in c:\builds\ELMAH\src\Elmah\ErrorSignal.cs:line 61
       at ElmahHttpErrorHandler.ProvideFault(Exception error, MessageVersion version, Message& fault) in c:\Myapplication\App_Code\Util\ElmahHttpErrorHandler.cs:line 19
       at System.ServiceModel.Dispatcher.ErrorBehavior.ProvideFault(Exception e, FaultConverter faultConverter, ErrorHandlerFaultInfo& faultInfo)
       at System.ServiceModel.Dispatcher.ErrorBehavior.ProvideMessageFaultCore(MessageRpc& rpc)
       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessageCleanup(MessageRpc& rpc)
  InnerException: 

Web.Config 文件

<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            </sectionGroup>
        </sectionGroup>
    </sectionGroup>
  <sectionGroup name="elmah">
    <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
    <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
    <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
    <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
  </sectionGroup>
</configSections>
<httpHandlers>
  <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
</httpModules>
...

<elmah>
  <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
  <security allowRemoteAccess="1" />
</elmah>

我用的是windows 7,有什么相关的问题吗?

更新 注意:我在 ASP.NET 下运行 WCF 服务。即我已通过添加新网站将 WCF 服务项目添加到解决方案中,然后选择 WCF 服务模板。我目前正在从 IDE 中运行它。不过,将其托管在 IIS 上也无济于事。

【问题讨论】:

    标签: c# wcf elmah exception-logging


    【解决方案1】:

    我正在回答我自己的问题。

    我尝试在我的 web.config 中添加以下内容

    <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    </system.serviceModel>
    

    还用以下属性装饰了我的服务

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]    
    public class CalculatorService : ICalculatorSession    
    {
        // Implement calculator service methods
     }
    

    还是没用。 然后我得到了解决方案here,通过它你可以在没有 HTTPContext 的情况下使用 Elmah。即通过写入记录错误

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

    而不是

    Elmah.ErrorSignal.FromCurrentContext().Raise(error);
    

    【讨论】:

    • 我遇到了同样的问题,问题是由于 WCF 项目中的应用程序实例为空。我想知道如果您在 Web 应用程序项目模板中运行服务,您会遇到同样的问题。
    • 所以你的意思是我将它用作网站项目而不是 Web 应用程序项目?
    • 不,我的意思是问题可能与 WCF 模板有关,但我错了
    • 这救了我,谢谢!您可能需要注意的另一件事。如果您使用 SQLErrorLog 类型,则必须在 errorLog 标记中指定 applicationName 键/值对,否则您将无法在日志中看到脱离上下文的条目,因为“默认”应用程序名称是从HTTPContext 存在时。如
    • 感谢@DavidC 提供的信息,我不知道。
    【解决方案2】:

    谢谢大家,这是我想出的 ErrorHelper 类,用于手动记录网站和 WCF 服务、Windows 服务等中的错误:

        public static class ErrorHelper
    {
        /// <summary>
        /// Manually log an exception to Elmah.  This is very useful for the agents that try/catch all the errors.
        /// 
        /// In order for this to work elmah must be setup in the web.config/app.config file
        /// </summary>
        /// <param name="ex"></param>
        public static void LogErrorManually(Exception ex)
        {
            if (HttpContext.Current != null)//website is logging the error
            {                
                var elmahCon = Elmah.ErrorSignal.FromCurrentContext();
                elmahCon.Raise(ex);
            }
            else//non website, probably an agent
            {                
                var elmahCon = Elmah.ErrorLog.GetDefault(null);
                elmahCon.Log(new Elmah.Error(ex));
            }
        }
    }
    

    【讨论】:

    • 正是我所追求的——谢谢
    • 对于尝试此操作并收到“无效参数”错误的任何其他人,请确保在您的服务/Windows 应用程序中添加对 System.Web 的引用。
    • System.Web 的引用救了我!谢谢@FirstDivision
    【解决方案3】:

    您是否在 ASP.NET 中运行此代码? 似乎 HttpContext 为空,这导致 ELmah 代码抛出异常 - 方法 Elmah.ErrorSignal.FromCurrentContext() 会将 HttpContext.Current 从我记得的内容(已经研究了一段时间)传递给 Elmah.ErrorSignal。 FromContext(HttpContext context) 方法,这似乎为空,在 Asp.Net 托管的东西中通常不会出现这种情况。

    另一种可能性是这发生在对服务器的请求之外,因此 HttpContext 可能为空

    【讨论】:

    • 我在 ASP.NET 下运行 WCF 服务。即我已通过添加新网站将 WCF 服务项目添加到解决方案,然后选择 WCF 服务模板。我目前正在从 IDE 中运行它。如果我将它托管在 IIS 上,它可以解决吗?
    • 我在 IIS 上运行它并遇到同样的问题
    【解决方案4】:

    我能找到的唯一稳定的解决方案是删除 web.config 属性并使用他们在Exception Logging for WCF Services using ELMAH 中使用的服务行为属性

    【讨论】:

    • +1 感谢您为解决已标记为临时解决方案答案的问题所做的努力。
    • 我自己也有同样的问题,所以不用担心
    • 也就是说,您几乎无法从 HTTP 请求本身获得调试 WCF 服务时所需的太多信息,因此我不确定是否值得。
    猜你喜欢
    • 2011-01-07
    • 2010-10-23
    • 2011-06-04
    • 1970-01-01
    • 2010-10-28
    • 2014-10-28
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多