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

  • Home
  • SEARCH
  • 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 6046373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:13:55+00:00 2026-05-23T07:13:55+00:00

Just started using log4net and trying to get my head around the config and

  • 0

Just started using log4net and trying to get my head around the config and logger hierarchy. Is this hierarchy based on namespaces or class and method/function hierarchy?

Lets say I have the following class structure…

public class MyClass
{
  private static readonly ILog log = LogManager.GetLogger(typeof(MyClass));

  public void Method1()
  {
    log4net.info("message");
  }

  public void Method2()
  {
    log4net.info("message");
  }

}

Is it possible to setup in the config for the log4net.info in method1 to use one appender and the log4net.info in method 2 to use another appender, even if they are off the same type e.g. SmtpAppender. If so how would the config look. here is my first attempt at it.

<appender name="SMTP1" type="log4net.Appender.SMTPAppender">
</appender>
<appender name="SMTP2" type="log4net.Appender.SMTPAppender">
</appender>


<logger name="MyClass.Method1">
    <level value="INFO" />
    <appender-ref ref="SMTP1" />
</logger>
<logger name="MyClass.Method2">
    <level value="INFO" />
    <appender-ref ref="SMTP2" />
</logger>
  • 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-23T07:13:56+00:00Added an answer on May 23, 2026 at 7:13 am

    The hierarchy is based on “names”. What does that mean?
    Well, you can specify a namespace in your logger xml (eg. Foo.Bar) and then fetch a logger for a class in that namespace using the GetLogger method which takes a Type. Any “sub” namespace under Foo.Bar will inherit Foo.Bar‘s logger config.
    Alternatively, you can fetch a logger based on any old string using the GetLogger method which takes a string.

    You can fetch loggers a couple of different ways. Most notably, by Type or by string.

    Being able to fetch by string, you really can name your loggers anything and fetch them using anything. What you currently have won’t work because log4net will fetch the logger based on the class… so you will be using the same logger for both methods.

    For what you want to do you have to create two loggers:

    public class MyClass
    {
      private static readonly ILog log = LogManager.GetLogger(typeof(MyClass).Name + "." + "Method1");
      private static readonly ILog log2 = LogManager.GetLogger(typeof(MyClass).Name + "." + "Method2");
    
      public void Method1()
      {
        log.info("message");
      }
    
      public void Method2()
      {
        log2.info("message");
      }
    }
    

    Here is the same logger xml file:

    <logger name="MyClass.Method1">
        <level value="INFO" />
        <appender-ref ref="SMTP1" />
    </logger>
    <logger name="MyClass.Method2">
        <level value="INFO" />
        <appender-ref ref="SMTP2" />
    </logger>
    

    I’m not too experienced with .Net, so maybe someone can find a better/more robust way to get the loggers for each method using some ninja Reflection, but that is the best I could do.

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

Sidebar

Related Questions

I just started using Log4Net and was looking to see what you have found
I have just started playing around with Log4Net... I now want to send an
Just started using this technique and am having strange results on the PC side.
I just started using Matplotlib and am trying to change the color of the
I just started using resharper and it barks at this generated assemblyinfo.cs file. Reason/Logic
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I just started using GNU Emacs as my text editor and I am concerned
I just started using the WPF WebBrowser that is included in Net 3.5 SP1.
I just started using SVN, and I have a cache directory that I don't

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.