ELMAH - exception logging without HttpContext

I tried this solution with Elmah.XmlFileErrorLog, but I get the following exception

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 File

 <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> 

I am using Windows 7. Are there any problems associated with this?

UPDATE Note: I am running the WCF service for ASP.NET. those. I added a WCF service project to the solution by adding a new website and then selecting a WCF service template. I am currently running it from the IDE. Hosting on IIS did not help either.

+44
c # wcf elmah exception-logging
Jun 08 '10 at 12:06 on
source share
4 answers

I answer my question.

I tried adding the following to my web.config

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

My service is also decorated with the following attribute

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

Still useless. Then I got a solution here , with which you can use Elmah without an HTTPContext. those. log errors by writing

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

instead

 Elmah.ErrorSignal.FromCurrentContext().Raise(error); 
+77
Jun 28 '10 at 13:00
source

Thanks to everyone, here is my ErrorHelper class, which I came up with to manually register errors on websites and in WCF services, Windows services, etc.:

  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)); } } } 
+32
Jun 06 '12 at 19:19
source

The only stable solution I could find was to remove the web.config attribute and use the service behavior attribute that they use in the Exception Log for WCF services using ELMAH

+1
Aug 10 2018-10-10
source

Do you use this code in ASP.NET? It seems that the HttpContext is NULL, and this causes the ELmah code to throw an exception. The Elmah.ErrorSignal.FromCurrentContext () method will pass the HttpContext.Current from what I remember (some time since studying it) to Elmah.ErrorSignal. FromContext (HttpContext context), and this seems to be null, which would usually not have been the case with materials hosted on Asp.Net

Another possibility is that this happens outside of the server request, so the HttpContext may be null

0
Jun 08 2018-10-06
source



All Articles