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 968839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:32:53+00:00 2026-05-16T02:32:53+00:00

Situation: I want to show the method and line number for the code that

  • 0

Situation: I want to show the method and line number for the code that logs a message. The problem is that I have an intermediate class which calls into the log4net logger. Unfortunately, due to existing architectural issues, I can’t do away with this intermediate class. The result is that I always see the method and line number as being in the intermediate class. Not what I want.

So I tried to create a custom PatternLayoutConverter, as per this question:

Does log4net support including the call stack in a log message

I am also programmatically configuring log4net since, again for architectural reasons, using an XML config file is not feasible (I also find it ridiculous that the preferred and only documented way of configuring log4net is through a stupid XML file, but that’s a topic for another discussion). So I followed this thread.

How to configure log4net programmatically from scratch (no config)

Everything works fine except that my custom converter is never called. Here is the code for the custom converter:

public class TVPatternLayout : PatternLayout {
    public TVPatternLayout() {
        this.AddConverter("logsite", typeof(TVPatternLayoutConverter));
    }
}

public class TVPatternLayoutConverter : PatternLayoutConverter {
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent) {
        StackTrace st = new StackTrace();
        int idx = 1;
        while(st.GetFrame(idx).GetMethod().DeclaringType.Assembly == typeof(LogManager).Assembly)
            idx++;
        writer.Write(String.Format("{0}.{1} (line {2})", st.GetFrame(idx).GetMethod().DeclaringType.Name, st.GetFrame(idx).GetMethod().Name,
                     st.GetFrame(idx).GetFileLineNumber()));
    }
}

And here is the code where I configure the logger:

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Configured = false;

RollingFileAppender appender = new RollingFileAppender();
appender.Name = loggerName;
appender.File = realPath;
appender.AppendToFile = true;
appender.MaximumFileSize = "8000";
appender.MaxSizeRollBackups = 2;

TVPatternLayout patternLayout = new TVPatternLayout();
patternLayout.ConversionPattern = logFormat;  // includes %logsite, my custom option
appender.Layout = patternLayout;

appender.ActivateOptions();
hierarchy.Root.AddAppender(appender);

hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
  • 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-05-16T02:32:54+00:00Added an answer on May 16, 2026 at 2:32 am

    Problem was that I forgot to call ActivateOptions() on the patternLayout. Naturally, I’d figure that out right after writing up a long question.

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

Sidebar

Related Questions

I have a situation where I want to create a signature of a data
I have a situation where I want to copy the output assembly from one
I've got a situation where I want to have a master list at the
I'm learning Python, and I have a situation where I want to consume items
I want to create maintainable code, but this inheritance situation is causing me problems.
Situation: I have a simple XML document that contains image information. I need to
Sometimes I find myself in the situation where I want to execute several sequential
I wonder what the best approach is for a situation where I want to
In my JSF 1.2 webapp I have a page with a <h:commandButton> that invokes
I have a situation where I need to check a couple of things before

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.