Closed shyamal890 closed 6 years ago
Is your error log configured correctly? What ErrorLog
implementation does ErrorLog.GetDefault(null)
return?
ErrorLog
is a inbuilt method of Elmah
comes when you download Elmah.bootstrapper
or Elmah
library.
ErrorLog.GetDefault(null)
is utilized to set HttpContext
to null
as call from Azure functions app
may not have a HttpContext
ErrorLog
is an abstract base class, not a method. You have to use/configure a concrete implementation to be used to log to some database. That's why I asked, what does ErrorLog.GetDefault(null)
return. What's your back-end database where are hoping it to log to?
Followed the guide in https://github.com/elmah/Bootstrapper to setup settings. When one adds elmah Bootstrapper it creates a Elmah.Ahtz.config
file in the solution folder. Which I haven't modified.
The only setting I added is to log errors in database. Here is the connectionString
added to Web.Config
file.
<add name="elmah:sql" connectionString="Data Source=SHYAMALPC;Initial Catalog=elmah;Integrated Security=SSPI;"
ErrorLog.GetDefault(null)
though creates an In-memory Log, wonder how to set it up to log to database.
From the sound of it, looks like you did the right setup but obviously it's still not working so I wonder if it's got to do without your environment.
ErrorLog.GetDefault(null)
though creates an In-memory Log, wonder how to set it up to log to database.
This is what I suspected.
Seems to me that Bootstrapper's start-up code is either not getting called or it's not picking up your configuration. Any chance you can debug through that piece to see if it's getting called?
Also, what's the version of ASP.NET you are targeting?
@atifaziz Targeting .Net Framework 4.6
. Don't know how to debug into a DLL.
I suggest you create the simplest, even empty, ASP.NET 4.6 Web Application project, add elmah.bootstrapper
package and use the following configuration (replacing the YOUR_*
bits):
<appSettings>
<add key="elmah:sql:applicationName" value="YOUR_APP_NAME"/>
</appSettings>
<connectionStrings>
<add name="elmah:sql" connectionString="Server=YOUR_SERVER_NAME;Database=YOUR_DB_NAME;Trusted_Connection=True;"/>
</connectionStrings>
Do the errors get logged?
So the referenced project which had elmah boostrapper setup is referenced by another parent webApi project where ErrorLog.GetDefault(null)
points correctly to the database with applicationName
. While when the project is referened in azure functions, ErrorLog.GetDefault(null)
fails to correctly point to the sql database.
Does the referenced project look for settings in the parent project?
Also tried creating new project as suggested and referencing it in azure functions, same issue: ErrorLog.GetDefault(null)
fails to correctly point to the sql database.
@atifaziz Any thoughts?
ELMAH & its Bootstrapper are primarily designed to be used in ASP.NET web applications. If you're using it for Azure Functions then you're on your own, I'm afraid, and what you're observing seems normal. You'll have to do your own setup (pretty similar to how Bootstrapper does it for you) to have ErrorLog.GetDefault(null)
return the right ErrorLog
implementation. This can be done with ServiceCenter
at run-time. For more information, see my reply in the “How to set the SqlErrorLog ConnectionString at runtime” thread over at the ELMAH Discussion Group.
Does the referenced project look for settings in the parent project?
There's no concept of a project at run-time so no.
Thanks that worked!
public static void Config()
{
var currentServiceCenter = Elmah.ServiceCenter.Current;
Elmah.ServiceCenter.Current = context =>
{
var container = new ServiceContainer(currentServiceCenter(context));
var connectionString = ConfigurationManager.ConnectionStrings["ErrorLog"].ToString();
var log = new SqlErrorLog(connectionString)
{
ApplicationName = "appName",
};
container.AddService(typeof(ErrorLog), log);
return container;
};
}
Azure functions app references a asp.net data service project assembly. This assembly has ELMAH setup to log manual errors like:
Moreover, all service calls are wrapped with try,catch block. So whenever azure functions app calls a function from the reference project, incase there's an error it would be caught by ELMAH and logged in the database.
However, I am not able to see any new entry in elmah's table.
Example of service function:
Also tried installing
elmah.bootstrapper
nuget package instead ofelmah
. That too didn't work