Using ELMAH without HttpContext

4299 views
Skip to first unread message

Nick

unread,
Jan 11, 2010, 7:37:03 PM1/11/10
to ELMAH
Is it possible to use ELMAH to log errors in say a console app? I
haven't had too much luck getting this to work. Anybody have any
pointers.

Atif Aziz

unread,
Jan 12, 2010, 2:23:51 AM1/12/10
to el...@googlegroups.com
Hi Nick,

Technically, it is possible to get ELMAH to log exception in a console application but obviously the HTTP modules and handlers won't do anything automagically. There is nothing preventing you from creating an instance of, for example, SqlErrorLog and calling its Log method. If you want to pick up the log currently configured, you can use ErrorLog.GetDefault(null) where null is allowed for HttpContext. However,I introduced a regression bug recently when working on issue 149 that prevented from null being passed to GetDefault but I've fixed that now. With that said, here's an example in showing ELMAH being used to log an error using IronPython's interactive interpreter on the console:

IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4200
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference('Elmah')
>>> from Elmah import Error, ErrorLog
>>> from System import ApplicationException
>>> from System.Collections import ArrayList
>>> log = ErrorLog.GetDefault(None)
>>> print log
<Elmah.MemoryErrorLog object at 0x000000000000002B [Elmah.MemoryErrorLog]>
>>> log.Log(Error(ApplicationException()))
'79e8f86c-e82f-4051-949c-14b8a8bdcf03'
>>> errors = ArrayList()
>>> total = log.GetErrors(0, 10, errors)
>>> print 'Total =', total, '; Count =', errors.Count
Total = 1 ; Count = 1
>>> error = errors[0]
>>> print error.Id, error.Error.Message
79e8f86c-e82f-4051-949c-14b8a8bdcf03 Error in the application.

- Atif 

--
You received this message because you are subscribed to the Google Groups "ELMAH" group.
To post to this group, send email to el...@googlegroups.com.
To unsubscribe from this group, send email to elmah+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/elmah?hl=en.




Nick Berardi

unread,
Jan 12, 2010, 8:41:13 AM1/12/10
to el...@googlegroups.com
I still understand the need for IIS.  But I have some non-web, outside processing tools, that need to be logged and it seemed easier to just put all the logging in the same place.  So that was the reason for my question.

mhadamji

unread,
Jan 26, 2010, 7:00:24 AM1/26/10
to ELMAH
Hi Atif
Your suggested method of calling below works well using XML logger.
Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));
When I switch to SQL Logger it does not log any entry. Any Ideas?

On Jan 12, 7:23 am, Atif Aziz <aziza...@gmail.com> wrote:
> Hi Nick,
>
> Technically, it is possible to get ELMAH to log exception in a console
> application but obviously the HTTP modules and handlers won't do anything
> automagically. There is nothing preventing you from creating an instance of,
> for example, SqlErrorLog and calling its Log method. If you want to pick up
> the log currently configured, you can use ErrorLog.GetDefault(null) where
> null is allowed for HttpContext. However,I introduced a regression bug
> recently when working on issue

> 149<http://code.google.com/p/elmah/issues/detail?id=149> that


> prevented from null being passed to GetDefault but I've

> fixed<http://code.google.com/p/elmah/source/detail?r=701>that now.

> > elmah+un...@googlegroups.com <elmah%2Bunsu...@googlegroups.com>.


> > For more options, visit this group at

> >http://groups.google.com/group/elmah?hl=en.- Hide quoted text -
>
> - Show quoted text -

Nick Berardi

unread,
Jan 26, 2010, 10:55:02 AM1/26/10
to el...@googlegroups.com
Same issue that I found.  XML seems to work fine, but SQL doesn't work for me.

To unsubscribe from this group, send email to elmah+un...@googlegroups.com.

Atif Aziz

unread,
Jan 26, 2010, 2:28:11 PM1/26/10
to el...@googlegroups.com
Can you guys report this as an issue with details to reproduce and version in question?
 
We have many SQL-based stores supported in ELMAH. I'm not sure if SQL means MS SQL Server or just all SQL-based loggers.

Atif Aziz

unread,
Jan 26, 2010, 2:30:17 PM1/26/10
to el...@googlegroups.com
I just run a test with SqlErrorLog backed by a database in SQL Server Express from the console and it seems to be working fine...
 
IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4200
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference('Elmah')
>>> from System import ApplicationException
>>> from System.Collections import ArrayList
>>> from Elmah import Error, ErrorLog, SqlErrorLog
>>> log = SqlErrorLog(r'Server=.\SQLEXPRESS;Database=ELMAH;Trusted_Connection=True')
>>> log.ApplicationName = 'TestApp'
>>> log.Log(Error(ApplicationException()))
'5c92599d-46e3-4fdf-8a29-4411c214caaa'

>>> errors = ArrayList()
>>> total = log.GetErrors(0, 10, errors)
>>> print 'Total =', total, '; Count =', errors.Count
Total = 1 ; Count = 1
>>> error = errors[0]
>>> print error.Id, error.Error.Message
5C92599D-46E3-4FDF-8A29-4411C214CAAA Error in the application.

- Atif
On Tue, Jan 26, 2010 at 4:55 PM, Nick Berardi <nber...@gmail.com> wrote:

mhadamji

unread,
Jan 26, 2010, 8:42:04 PM1/26/10
to ELMAH
Thanks Atif
I am using MS-SQL 2005 developer edition with following connection
string.
<errorLog type="Elmah.SqlErrorLog, Elmah"
connectionString="Server=.;Database=elmahdb;Trusted_Connection=True;"/
>

The following error gets logged when called as below.
Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));

However it does not get displayed in the elmah.axd file.

Looking at the source code in SqlErrorLog.cs line 86
string appName = Mask.NullString((string)config["applicationName"]);
The above connection string has not been passed with application name
and results in string.emtpy. The stored procedure fails when called
with application name as emtpty string.

I changed the web.config to
<errorLog type="Elmah.SqlErrorLog, Elmah"
connectionString="Server=.;Database=elmahdb;Trusted_Connection=True;"
applicationName="MyApp"/>

Having set the application name in the config file, logs and displays
all the errors in the display logs correctly.

Setting the applicationName has resolved my problem.

Thanks


On Jan 26, 7:30 pm, Atif Aziz <aziza...@gmail.com> wrote:
> I just run a test with SqlErrorLog backed by a database in SQL Server
> Express from the console and it seems to be working fine...
>
> IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4200
> Type "help", "copyright", "credits" or "license" for more information.>>> import clr
> >>> clr.AddReference('Elmah')
> >>> from System import ApplicationException
> >>> from System.Collections import ArrayList
> >>> from Elmah import Error, ErrorLog, SqlErrorLog
> >>> log =
>
> SqlErrorLog(r'Server=.\SQLEXPRESS;Database=ELMAH;Trusted_Connection=True')>>> log.ApplicationName = 'TestApp'
> >>> log.Log(Error(ApplicationException()))
>
> '5c92599d-46e3-4fdf-8a29-4411c214caaa'>>> errors = ArrayList()
> >>> total = log.GetErrors(0, 10, errors)
> >>> print 'Total =', total, '; Count =', errors.Count
>
> Total = 1 ; Count = 1>>> error = errors[0]
> >>> print error.Id, error.Error.Message
>
> 5C92599D-46E3-4FDF-8A29-4411C214CAAA Error in the application.
>
> - Atif
>
>
>
> On Tue, Jan 26, 2010 at 4:55 PM, Nick Berardi <nbera...@gmail.com> wrote:
> > Same issue that I found.  XML seems to work fine, but SQL doesn't work for
> > me.
>

> >> elmah%2Bunsu...@googlegroups.com<elmah%252Buns...@googlegroups.com­>


> >> >.
> >> > > For more options, visit this group at

> >> > >http://groups.google.com/group/elmah?hl=en.-Hide quoted text -


>
> >> > - Show quoted text -
>
> >> --
> >>  You received this message because you are subscribed to the Google
> >> Groups "ELMAH" group.
> >> To post to this group, send email to el...@googlegroups.com.
> >> To unsubscribe from this group, send email to

> >> elmah+un...@googlegroups.com <elmah%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at

> >>http://groups.google.com/group/elmah?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "ELMAH" group.
> > To post to this group, send email to el...@googlegroups.com.
> > To unsubscribe from this group, send email to

Atif Aziz

unread,
Jan 28, 2010, 3:31:32 PM1/28/10
to el...@googlegroups.com
Glad to hear you could get it solved. Your solution is also consistent with the test I did earlier where I specifically did set the application name. If you don't set it then the problem is that ELMAH cannot infer it automatically because there is no context available. See ErrorLog.InferApplicationName for details:
 
 
ELMAH thereofre ends up using a blank for application name. The error is probably getting logged but you can't see it through the views because they filter the log based on an application name. Since the views are being served in response to a web request, the context is available for inference and a different application name is being used.
 
Hope this clears up things.
 
- Atif

To unsubscribe from this group, send email to elmah+un...@googlegroups.com.

Jed

unread,
Feb 14, 2010, 8:45:19 PM2/14/10
to ELMAH
An option for people without a context in Global.asax Session_End /
Application_End:

public class Global : System.Web.HttpApplication
{
protected void Session_End(object sender, EventArgs e)
{
Elmah.ErrorSignal.Get(this).Raise(...);
}
}

Original discussion of this as a documentation issue:

http://code.google.com/p/elmah/issues/detail?id=30

~Jed

Sivaram Boppana

unread,
Oct 29, 2013, 1:38:55 PM10/29/13
to el...@googlegroups.com
Hey Atif,

I tried using Elmah.ErrorLog.GetDefault(null).Log(e); but it is saying that the best overloaded method match for GetDefault(HttpContext) has some invalid arguments. You said that we can pass null to it. Since this post is so old, I can see that the design might have changed from then. The latest elmah that I got through Nuget are version 1.2.2. Do I have to do anything different to use this version to log in Console application? Please let me know.

Thanks,
Ram.

Atif Aziz

unread,
Oct 30, 2013, 3:47:58 AM10/30/13
to el...@googlegroups.com
What's the type of e? If it's Exception or a subclass of Exception you have to first wrap it into an Error like this:

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

- Atif
To unsubscribe from this group and stop receiving emails from it, send an email to elmah+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Sivaram Boppana

unread,
Oct 30, 2013, 11:41:32 AM10/30/13
to el...@googlegroups.com
Yes, I'm doing that. 

Elmah.ErrorLog.GetDefault(null).Log(new Error(new Exception("Process failed at ----")));

But It shows me a syntax error on GetDefault(null). (Just to remind you, I'm using this in a console application. We have other web projects that have elmah working fine in them.)

I even tried to use the sqllogger directly

Elmah.SqlErrorLog sqllogger = new SqlErrorLog(ConnectionString);
sqllogger.Log(new Error(new Exception("Log - Process Started")));

It allows me to do sqllogger but the log doesn't appear in the elmah db. 

Atif Aziz

unread,
Oct 30, 2013, 5:17:15 PM10/30/13
to el...@googlegroups.com
But It shows me a syntax error on GetDefault(null).

Can you share the syntax error exactly as emitted by the compiler?

- Atif

Sivaram Boppana

unread,
Oct 30, 2013, 5:34:11 PM10/30/13
to el...@googlegroups.com
Atif,

I'm sorry. I had to clean up my solution to get this thing working. I have some bad references I guess. It works with out errors now. But they are still not getting logged in the database. Do I have to add anything different for console apps in their app configs for elmah to work? If so, can you please share a link where I can look at a working app config file.

Thanks alot!

Ram.

Atif Aziz

unread,
Oct 30, 2013, 5:46:28 PM10/30/13
to el...@googlegroups.com
How are you checking if the errors are logged? Are you checking directly in the DB tables or using the web UI supplied with ELMAH?

- Atif

Sivaram Boppana

unread,
Oct 30, 2013, 5:52:34 PM10/30/13
to el...@googlegroups.com
I'm looking in to the DB tables in Elmah_Error table. I suspect the issue might be the config file. I got the settings from a web project and used in the app config.

Atif Aziz

unread,
Oct 30, 2013, 6:25:56 PM10/30/13
to el...@googlegroups.com
You can turn on standard tracing in .NET (see MDSN docs) to see if ELMAH is having trouble with logging. Alternatively, if you have the debugger attached to your console app, you will see a trace message in the debug output pane if ELMAH is having trouble logging.

- Atif

Sivaram Boppana

unread,
Oct 31, 2013, 3:30:40 PM10/31/13
to el...@googlegroups.com
Nothing in trace or Output window. When I debug through elmah error log line, it just passes them as it executed them successfully and I cannot see those logs in db. Can you please share the config file of a working console application if you have any?

Sivaram Boppana

unread,
Oct 31, 2013, 4:15:04 PM10/31/13
to el...@googlegroups.com
Atif,

I got it working. I was just doing too much in the app config. ConfigSections, elmah, connectionstrings are the only sections required for console application. Thanks a lot for your time!

Ram.

Srdjan Toholj

unread,
Apr 8, 2020, 3:40:12 PM4/8/20
to ELMAH
Is it possible to check elmah log crated by .net console app in web UI?

Atif Aziz

unread,
Apr 8, 2020, 3:45:16 PM4/8/20
to el...@googlegroups.com
Yes, it is. You just need make sure, at least speaking for SqlErrorLog, that the application name of the logged error matches that configured for the web app.

Reply all
Reply to author
Forward
0 new messages