Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:43:43+00:00 2026-06-15T18:43:43+00:00

This is probably not possible, but I’m wondering if there’s a way to make

  • 0

This is probably not possible, but I’m wondering if there’s a way to make it work.

I have a large ASP.NET MVC43 application that is already instrumented with logging statements.

Now we need to include a “Company Name” value from the Session object in each log entry. Is it possible to configure log4net to read Session data and include it in the log entry? Or some way to force it to…?

Thanks for any ideas.

[Edit]
This question helped a lot: How can I include SessionID in log files using log4net in ASP.NET?

I ended up with this as my solution:

In Global.asax.cs:

    // After the session is acquired, push the organization code into log4net's thread context, in case it has to log anything.
    protected void Application_PostAcquireRequestState(object sender, EventArgs e)
    {
        if (Context.Handler is IRequiresSessionState && Session != null && Session[Constants.EMPLOYEE_DETAILS] != null)
                log4net.ThreadContext.Properties["Company"] = ((EmployeeDetails)Session[Constants.EMPLOYEE_DETAILS]).Company;
    }

In log4net configuration:

  <parameter>
    <parameterName value="@Company"/>
    <dbType value="String"/>
    <size value="10"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{Company}" />
    </layout>
  </parameter>

Works great – I get a company name in my log output now. Thanks for the help, everyone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:43:44+00:00Added an answer on June 15, 2026 at 6:43 pm

    See this answer for some ideas on how you can implement this.

    Capture username with log4net

    You should be able to do it without implementing your own Appender.

    Here is an example of a custom PatternLayoutConverter that pulls a parameterized value from HttpContext.Current.Session. (I don’t remember if tested it when I posted as part of the linked answer or not, but it should be close):

    namespace Log4NetTest
    {
      class HttpContextSessionPatternConverter : PatternLayoutConverter
      {
        protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)
        {
          //Use the value in Option as a key into HttpContext.Current.Session
          string setting = "";
    
          HttpContext context = HttpContext.Current;
          if (context != null)
          {
            object sessionItem;
            sessionItem = context.Session[Option];
            if (sessionItem != null)
            {
              setting = sessionItem.ToString();
            }
            writer.Write(setting);
          }
        }
      }
    }
    

    Another simpler idea would be to do something like this… Create an object that will retrieve the Session parameter you want and put that object in the MDC, then reference the MDC in your layout. When log4net accesses the object from the MDC, it will call its ToString method to get the value to be written to the log.

    public class HttpContextSessionParametreProvider
    {
      private string _name;
      private string _notSet;
    
      public HttpContextSessionParameterProvider(string name)
      {
        _name = name;
        _notSet = string.Format("{0} not set", _name);
      }
    
      public override string ToString()
      {
        HttpContext context = HttpContext.Current;  
        if (context != null && context.Session != null)
        {
          object item = context.Session[_name];
          if (item != null)
          {
            return item.ToString();
          }
        }
        return _notSet;
      }
    }
    

    Use it like this somewhere near the entry point of your program:

    MDC.Set("CompanyName", new HttpContextSessionParametreProvider("Company Name"));
    

    (I don’t have an example handy for how to configure log4net to pull a parameter from the MDC, but it should not be hard to find one).

    EDIT:

    You could configure your PatternLayout something like this:

      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%thread]|[%property{CompanyName}]|%message%newline"/>
      </layout>
    

    Now, assuming that HttpContext.Session["Company Name"] is set to something, whenever you log a message, the value will be written to the log.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure there's a clean way to do this, but I'm probably not using
I know this is probably not possible but let's say I have a model
This is probably not possible with CSS, but maybe I'm wrong: I have a
This is probably not possible, but I have this class: public class Metadata<DataType> where
I still can't belive this is not possible but is there a way to
I have a feeling that this probably is not possible using strictly CSS, but,
This is weird and probably not possible but I'll ask anyway. I'm making this
I'm not sure if this possible but probably is fairly simple. I've made a
This is probably not the wisest question, but is it possible to return a
I suspect this is probably not possible due to browser security, however I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.