-
Notifications
You must be signed in to change notification settings - Fork 8
Conversation
Removed the reference to HTTPContext so that the Log4Net configurator can be called from the Global.asax to support MVC projects
@jmachale could you please provide the error or issues you're seeing that required this change? I'm using the code as is in 4 mvc projects right now without issue. I'm fairly certain; and I can test again to confirm, that I originally had the code that you're requesting. It broke the database logger for ELMAH due to the way the handler checks the context to pull from. Thank you! Here is what I have in my global.asax for reference: public class MvcApplication : System.Web.HttpApplication
{
/// <summary>
/// Application Start Event
/// </summary>
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
ILog log = LogManager.GetLogger(typeof(MvcApplication));
log.Debug("Application Starting"); Here is what I have in my web.config for reference: <elmah>
<security allowRemoteAccess="1" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ModelContainer" applicationName="TrainingTrackerDev" />
</elmah>
<location path="elmah.axd" inheritInChildApplications="false">
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<!--
See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for
more information on using ASP.NET authorization securing ELMAH.
<authorization>
<allow roles="admin" />
<deny users="*" />
</authorization>
-->
</system.web>
<system.webServer>
<handlers>
<add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
</location>
<log4net>
<appender name="elmahappender" type="elmahappender_log4net.ELMAHAppender, elmahappender_log4net">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="elmahappender" />
</root>
</log4net> |
Hi Ed, Sorry I believed the behavoiur should be the same for all calls to GetLog. I am logging Elmah to a file not DB so that might be the difference. With the original code I call XmlConfigurator.Configure() in Application_Start() but that calls your Elmah appender which references HTTPContext, which does not exist at that point as there has been no request yet so an exception was being raised - I am not sure how you are not seeing this behaviour also (MVC4). Thanks, John. |
Have you tried doing assembly tagging? In the assemblyinfo.cs class:
I can look at this later today to try to reproduce your exception. Could you create a sample solution with the issue? If you don't have time I can try it with your parameters (even your web.config would put me on a good track) |
Hi Ed, Yes I tried assembly tagging initially but it wouldn’t work either. My config is as follows and should show the error if you create a default MVC 4 application and add Elmah, log4net.mvc and your appender from nuget and then just add XmlConfigurator.Configure(); in app_start with the following config ……
Thanks again, John. Have you tried doing assembly tagging? In the assemblyinfo.cs class: [assembly: log4net.Config.XmlConfigurator()] I can look at this later today to try to reproduce your exception. Could you create a sample solution with the issue? If you don't have time I can try it with your parameters (even your web.config would put me on a good track) — [Storm Technology Ltd.]http://www.linkedin.com/companies/516964 |
Ok, I think I found the problem, I was looking at an older version of the example MVC site this morning. After playing with it for a little bit, It's all dependant on when ActivateOptions is called, which is the first time that the LogManager is queried. Here's what the working site looks like: AssemblyInfo.cs: Global
This works as expected. If I comment out the class initialization of the variable and uncomment the line in the application_start, then I get the exception you're getting. The class is initialized in the context of a request, where as the application_start function is not, that's where the difference is coming in. The context is used by ELMAH to infer what the application name should be. I think the way I'll solve this so as to not break existing apps is to add a setting on whether to try to infer the application name or not. If you just pass in null to the context, the application name is blank and could mess up some of the ELMAH loggers that require application name to know which logs to display. I'll try to get that done tonight |
Ok thanks Ed. From: Ed [mailto:notifications@github.com] Ok, I think I found the problem, I was looking at an older version of the example MVC site this morning. After playing with it for a little bit, It's all dependant on when ActivateOptions is called, which is the first time that the LogManager is queried. Here's what the working site looks like: AssemblyInfo.cs: [assembly: log4net.Config.XmlConfigurator()] Global
This works as expected. If I comment out the class initialization of the variable and uncomment the line in the application_start, then I get the exception you're getting. The class is initialized in the context of a request, where as the application_start function is not, that's where the difference is coming in. The context is used by ELMAH to infer what the application name should be. I think the way I'll solve this so as to not break existing apps is to add a setting on whether to try to infer the application name or not. If you just pass in null to the context, the application name is blank and could mess up some of the ELMAH loggers that require application name to know which logs to display. I'll try to get that done tonight — [Storm Technology Ltd.]http://www.linkedin.com/companies/516964 |
Pushed to dev branch commit: 4b91caf if you want to give it a shot.
|
Hi Ed sorry for the delay – yes that works perfect for me thanks! I guess the only scenario it might not work for is if someone has a SQL and text file log as a backup in case the DB connection is down but that is not my current scenario anyway. Thanks again, John. From: Ed [mailto:notifications@github.com] Pushed to dev branch commit: 4b91cafhttps://github.com/edwinf/log4net---ELMAH-Appender/commit/4b91caf8a55495838aba5e247c734fafee9a6583 if you want to give it a shot.
— [Storm Technology Ltd.]http://www.linkedin.com/companies/516964 |
I should be able to manage that scenario by making the documentation clear about how it will initialize itself and when one might run into an issue. Code merged into main with pull request #3 |
This fix is not present in the Nuget package. |
@Robelind i believe it's in a prerelease version. I never changed the version number to be a stable version because I don't actually use this package anymore and never had time to fully test the changes. I didn't want to break anyone who was using it if there was some backwards compatibility issue. |
Removed the reference to HTTPContext so that the Log4Net configurator can be called from the Global.asax to support MVC projects