Elmah-例外记录而没有HTTPContext
-
24-10-2019 - |
题
我试过了 这个 使用elmah.xmlfileerrorlog解决方案,但我正在遵循异常
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>
我正在使用Windows7。可以有任何问题吗?
更新注意:我正在运行WCF Service Unders ASP.NET。即,我已经添加了WCF服务项目来通过添加新网站,然后选择WCF服务模板。我目前从IDE内部运行它。在IIS上托管它也没有帮助。
解决方案
我正在回答自己的问题。
我尝试在我的web.config中添加以下内容
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
还用以下属性装饰了我的服务
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CalculatorService : ICalculatorSession
{
// Implement calculator service methods
}
仍然没有用。然后我得到了解决方案 这里 您可以在没有httpcontext的情况下使用Elmah。 IE编写日志错误
Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));
代替
Elmah.ErrorSignal.FromCurrentContext().Raise(error);
其他提示
谢谢大家,这是我想到的ErrorHelper课程,我想到了在网站和WCF服务,Windows Services等中手动记录错误:
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));
}
}
}
我能找到的唯一稳定的解决方案是删除web.config属性并使用他们在 使用Elmah的WCF服务的例外记录
您是否在ASP.NET中运行此代码?似乎httpcontext是无效的,这导致Elmah代码抛出异常-Elmah.ermah.erorsignal.fromcurrentContext()将通过我记得的(自从研究以来一直在研究)传递到Elmah.ermah.ermah.errorsignal。 FromContext(httpcontext上下文)方法,这似乎是null,通常在ASP.NET托管的内容中并非如此。
另一种可能性是,这是在对服务器请求之外发生的,因此httpcontext可能为null
不隶属于 StackOverflow