Skip to content

Commit 3a99d50

Browse files
committed
In IIS integrated mode Elmah fails at application start (Can't access HttpContext.Current.Request)
A workaround is described here: http://stackoverflow.com/a/2196594/618331 Just wrapped the normal Elmah log statements in try-catch and added the workaround Elmah log statements in the catch{} blocks. Can't create a real test for it, because it happens only at Application_start... A dummy global.asax.cs: protected void Application_Start() { try { //do something... } catch(Exception exc) { //try logging logger.Error("something went wrong...", exc); //this is line 50 } } The exception: System.Web.HttpException (0x80004005): Request is not available in this context at System.Web.HttpContext.get_Request() at Elmah.ErrorLog.InferApplicationName(HttpContext context) at Elmah.ErrorLog.GetDefaultImpl(HttpContext context) at Elmah.ServiceContainer.GetService(Type serviceType) at Elmah.ErrorLog.GetDefault(HttpContext context) at ServiceStack.Logging.Elmah.ElmahInterceptingLogger.Error(Object message) at x.Application_Start() in x\Global.asax.cs:line 50 Also had to update Nuget.exe because The schema version of 'NLog' is incompatible with version 1.6.21205.9031 of NuGet. Please upgrade NuGet to the latest version from http://go.microsoft.com/fwlink/?LinkId=213942.
1 parent 4461970 commit 3a99d50

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/.nuget/NuGet.exe

132 KB
Binary file not shown.

src/ServiceStack.Logging.Elmah/ElmahInterceptingLogger.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,67 @@ public void DebugFormat(string format, params object[] args)
4343

4444
public void Error(object message, Exception exception)
4545
{
46-
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(exception, HttpContext.Current));
46+
try {
47+
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(exception, HttpContext.Current));
48+
}
49+
catch {
50+
ErrorLog.GetDefault(null).Log(new Error(exception));
51+
}
4752
log.Error(message, exception);
4853
}
4954

5055
public void Error(object message)
5156
{
52-
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(message.ToString()), HttpContext.Current));
57+
try {
58+
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(message.ToString()), HttpContext.Current));
59+
}
60+
catch {
61+
ErrorLog.GetDefault(null).Log(new Error(new System.ApplicationException(message.ToString())));
62+
}
5363
log.Error(message);
5464
}
5565

5666
public void ErrorFormat(string format, params object[] args)
5767
{
58-
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(string.Format(format, args)), HttpContext.Current));
68+
try {
69+
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(string.Format(format, args)), HttpContext.Current));
70+
}
71+
catch {
72+
ErrorLog.GetDefault(null).Log(new Error(new System.ApplicationException(string.Format(format, args))));
73+
}
5974
log.ErrorFormat(format, args);
6075
}
6176

6277
public void Fatal(object message, Exception exception)
6378
{
64-
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(exception, HttpContext.Current));
79+
try {
80+
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(exception, HttpContext.Current));
81+
}
82+
catch {
83+
ErrorLog.GetDefault(null).Log(new Error(exception));
84+
}
6585
log.Fatal(message, exception);
6686
}
6787

6888
public void Fatal(object message)
6989
{
70-
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(message.ToString()), HttpContext.Current));
90+
try {
91+
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(message.ToString()), HttpContext.Current));
92+
}
93+
catch {
94+
ErrorLog.GetDefault(null).Log(new Error(new System.ApplicationException(message.ToString())));
95+
}
7196
log.Fatal(message);
7297
}
7398

7499
public void FatalFormat(string format, params object[] args)
75100
{
76-
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(string.Format(format, args)), HttpContext.Current));
101+
try {
102+
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(new System.ApplicationException(string.Format(format, args)), HttpContext.Current));
103+
}
104+
catch {
105+
ErrorLog.GetDefault(null).Log(new Error(new System.ApplicationException(string.Format(format, args))));
106+
}
77107
log.FatalFormat(format, args);
78108
}
79109

0 commit comments

Comments
 (0)