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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:13:51+00:00 2026-05-10T21:13:51+00:00

Currently I have the function CreateLog() for creating a a log4net Log with name

  • 0

Currently I have the function CreateLog() for creating a a log4net Log with name after the constructing instance’s class. Typically used as in:

class MessageReceiver {      protected ILog Log = Util.CreateLog();      ... } 

If we remove lots of error handling the implementation boils down to: [EDIT: Please read the longer version of CreateLog further on in this post.]

public ILog CreateLog() {         System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(1);         System.Reflection.MethodBase method = stackFrame.GetMethod();         return CreateLogWithName(method.DeclaringType.FullName); } 

Problem is that if we inheirit MessageReceiver into sub classes the log will still take its name from MessageReceiver since this is the declaring class of the method (constructor) which calls CreateLog.

class IMReceiver : MessageReceiver { ... }  class EmailReceiver : MessageReceiver { ... } 

Instances of both these classes would get Logs with name ‘MessageReceiver’ while I would like them to be given names ‘IMReceiver’ and ‘EmailReceiver’.

I know this can easily be done (and is done) by passing a reference to the object in creation when calling CreateLog since the GetType() method on object does what I want.

There are some minor reasons to prefer not adding the parameter and personally I feel disturbed by not finding a solution with no extra argument.

Is there anyone who can show me how to implement a zero argument CreateLog() that gets the name from the subclass and not the declaring class?

EDIT:

The CreateLog function does more than mentioned above. The reason for having one log per instance is to be able to differ between different instances in the logfile. This is enforced by the CreateLog/CreateLogWithName pair.

Expanding on the functionality of CreateLog() to motivate its existence.

public ILog CreateLog() {         System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(1);         System.Reflection.MethodBase method = stackFrame.GetMethod();         Type type = method.DeclaringType;          if (method.IsStatic)         {             return CreateLogWithName(type.FullName);         }         else         {             return CreateLogWithName(type.FullName + '-' + GetAndInstanceCountFor(type));         } } 

Also I prefer writing ILog Log = Util.CreateLog(); rather than copying in some long cryptic line from an other file whenever I write a new class. I am aware that the reflection used in Util.CreateLog is not guaranteed to work though – is System.Reflection.MethodBase.GetCurrentMethod() guaranteed to work?

  • 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. 2026-05-10T21:13:52+00:00Added an answer on May 10, 2026 at 9:13 pm

    Is there anyone who can show me how to implement a zero argument CreateLog() that gets the name from the subclass and not the declaring class?

    I don’t think you’ll be able to do it by looking at the stack frame.

    While your class is IMReceiver, the call to CreateLog method is in the MessageReceiver class. The stack frame must tell you where the method is being called from, or it wouldn’t be any use, so it’s always going to say MessageReceiver

    If you called CreateLog explicitly in your IMReceiver and other classes, then it works, as the stack frame shows the method being called in the derived class (because it actually is).

    Here’s the best thing I can come up with:

    class BaseClass{   public Log log = Utils.CreateLog(); } class DerivedClass : BaseClass {   public DerivedClass() {     log = Utils.CreateLog();   } } 

    If we trace creation of logs, we get this:

    new BaseClass(); # Log created for BaseClass  new DerivedClass(); # Log created for BaseClass # Log created for DerivedClass 

    The second ‘log created for derived class’ overwrites the instance variable, so your code will behave correctly, you’ll just be creating a BaseClass log which immediately gets thrown away. This seems hacky and bad to me, I’d just go with specifying the type parameter in the constructor or using a generic.

    IMHO specifying the type is cleaner than poking around in the stack frame anyway

    If you can get it without looking at the stack frame, your options expand considerably

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One possibility is a time stamp issue. Can you check… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer You can use a respond_to stanza in your controller method,… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer After the Pentium I or II, most optimizations performed by… May 11, 2026 at 11:35 pm

Related Questions

After trying to avoid JavaScript for years, Iv started using Query for validation in
I'm looking to create a generic confirmation box that can be used by multiple
I currently have the below code to fetch data from my Entity framework model.
I'm currently working on a very short project on Prolog, and just got stuck

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.