Skip to content
This repository has been archived by the owner on Jan 2, 2018. It is now read-only.

Removed reference to HTTPContext #2

Closed
wants to merge 1 commit into from

Conversation

jmachale
Copy link

@jmachale jmachale commented Jan 9, 2013

Removed the reference to HTTPContext so that the Log4Net configurator can be called from the Global.asax to support MVC projects

Removed the reference to HTTPContext so that the Log4Net configurator can be called from the Global.asax to support MVC projects
@edwinf
Copy link
Owner

edwinf commented Jan 9, 2013

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

@jmachale
Copy link
Author

jmachale commented Jan 9, 2013

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).
With the original code if I move XmlConfigurator.Configure() to a Controller action for a test it works fine.

Thanks,

John.

@edwinf
Copy link
Owner

edwinf commented Jan 9, 2013

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)

@jmachale
Copy link
Author

jmachale commented Jan 9, 2013

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

……
<appender name="ElmahAppender" type=".ELMAHAppender">
  <threshold value="ERROR"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

<root>
  <level value="ALL" />
  <appender-ref ref="RollingFileAppender" />
  <appender-ref ref="ElmahAppender" />
</root>

Thanks again,

John.
From: Ed [mailto:notifications@github.com]
Sent: 09 January 2013 14:21
To: edwinf/log4net---ELMAH-Appender
Cc: John MacHale
Subject: Re: [log4net---ELMAH-Appender] Removed reference to HTTPContext (#2)

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)


Reply to this email directly or view it on GitHubhttps://github.com//pull/2#issuecomment-12045874.

[Storm Technology Ltd.]http://www.linkedin.com/companies/516964

@edwinf
Copy link
Owner

edwinf commented Jan 9, 2013

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

    public class MvcApplication : System.Web.HttpApplication
    {
        private ILog log = LogManager.GetLogger(typeof(MvcApplication));

        protected void Application_Start()
        {
            //ILog log = LogManager.GetLogger(typeof(MvcApplication));
            log.Error("It made it here and logged it to elmah");

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

@jmachale
Copy link
Author

jmachale commented Jan 9, 2013

Ok thanks Ed.

From: Ed [mailto:notifications@github.com]
Sent: 09 January 2013 17:36
To: edwinf/log4net---ELMAH-Appender
Cc: John MacHale
Subject: Re: [log4net---ELMAH-Appender] Removed reference to HTTPContext (#2)

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

public class MvcApplication : System.Web.HttpApplication

{

    private ILog log = LogManager.GetLogger(typeof(MvcApplication));



    protected void Application_Start()

    {

        //ILog log = LogManager.GetLogger(typeof(MvcApplication));

        log.Error("It made it here and logged it to elmah");

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


Reply to this email directly or view it on GitHubhttps://github.com//pull/2#issuecomment-12056863.

[Storm Technology Ltd.]http://www.linkedin.com/companies/516964

@edwinf
Copy link
Owner

edwinf commented Jan 9, 2013

Pushed to dev branch commit: 4b91caf if you want to give it a shot.

<appender name="elmahappender" type="elmahappender_log4net.ELMAHAppender, elmahappender_log4net">
     <UseNullContext value="True" />
     <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
</appender>

@jmachale
Copy link
Author

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]
Sent: 09 January 2013 18:35
To: edwinf/log4net---ELMAH-Appender
Cc: John MacHale
Subject: Re: [log4net---ELMAH-Appender] Removed reference to HTTPContext (#2)

Pushed to dev branch commit: 4b91cafhttps://github.com/edwinf/log4net---ELMAH-Appender/commit/4b91caf8a55495838aba5e247c734fafee9a6583 if you want to give it a shot.

 <UseNullContext value="True" />

 <layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />


Reply to this email directly or view it on GitHubhttps://github.com//pull/2#issuecomment-12059566.

[Storm Technology Ltd.]http://www.linkedin.com/companies/516964

@edwinf
Copy link
Owner

edwinf commented Jan 10, 2013

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

@edwinf edwinf closed this Jan 10, 2013
@Robelind
Copy link

Robelind commented Feb 5, 2015

This fix is not present in the Nuget package.
Would be nice if it was.

@edwinf
Copy link
Owner

edwinf commented Feb 5, 2015

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants