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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:23:21+00:00 2026-05-11T21:23:21+00:00

Background I am writing a class library assembly in C# .NET 3.5 which is

  • 0

Background

I am writing a class library assembly in C# .NET 3.5 which is used for integration with other applications including third-party Commercial-Off-The-Shelf (COTS) tools. Therefore, sometimes this class library will be called by applications (EXEs) that I control while other times it will be called by other DLLs or applications that I do not control.

Assumptions

  • I am using C# 3.0, .NET 3.5 SP1, and Visual Studio 2008 SP1
  • I am using log4net 1.2.10.0 or greater

Constraints

Any solution must:

  • Allow for the class library to enable and configure logging via it’s own configuration file, if the calling application does not configure log4net.
  • Allow for the class library to enable and configuring logging via the calling applications configuration, if it specifies log4net information

OR

  • Allow for the class library to enable and configuring logging using it’s own configuration file at all times.

Problem

When my stand-alone class library is called by a DLL or application that I do not control (such as a third-party COTS tool) and which doesn’t specify log4net configuration information, my class library is unable to do any of it’s logging.


Question

How do you configure and enable log4net for a stand-alone class library assembly so that it will log regardless if the calling application provides log4net configuration?

  • 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-11T21:23:22+00:00Added an answer on May 11, 2026 at 9:23 pm

    Solution 1

    A solution for the first set of constraints is to basically wrap the log4net.LogManager into your own custom LogManager class like Jacob, Jeroen, and McWafflestix have suggested (see code below).

    Unfortunately, the log4net.LogManager class is static and C# doesn’t support static inheritance, so you couldn’t simply inherit from it and override the GetLogger method.
    There aren’t too many methods in the log4net.LogManager class however, so this is certainly a possibility.

    The other drawback to this solution is that if you have an existing codebase (which I do in my case) you would have to replace all existing calls to log4net.LogManager with your wrapper class. Not a big deal with today’s refactoring tools however.

    For my project, these drawbacks outweighed the benefits of using a logging configuration supplied by the calling application so, I went with Solution 2.

    Code

    First, you need a LogManager wrapper class:

    using System;
    using System.IO;
    using log4net;
    using log4net.Config;
    
    namespace MyApplication.Logging
    {
        //// TODO: Implement the additional GetLogger method signatures and log4net.LogManager methods that are not seen below.
        public static class LogManagerWrapper
        {
            private static readonly string LOG_CONFIG_FILE= @"path\to\log4net.config";
    
            public static ILog GetLogger(Type type)
            {
                // If no loggers have been created, load our own.
                if(LogManager.GetCurrentLoggers().Length == 0)
                {
                    LoadConfig();
                }
                return LogManager.GetLogger(type);
            }
    
            private void LoadConfig()
            {
               //// TODO: Do exception handling for File access issues and supply sane defaults if it's unavailable.   
               XmlConfigurator.ConfigureAndWatch(new FileInfo(LOG_CONFIG_FILE));
            }              
    }
    

    Then in your classes, instead of:

    private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
    

    Use:

    private static readonly ILog log = LogManagerWrapper.GetLogger(typeof(MyApp));
    

    Solution 2

    For my purposes, I have decided to settle on a solution that meets the second set of constraints. See the code below for my solution.

    From the Apache log4net document:

    “An assembly may choose to utilize a named logging repository rather than the default repository. This completely separates the logging for the assembly from the rest of the application. This can be very useful to component developers that wish to use log4net for their components but do not want to require that all the applications that use their component are aware of log4net. It also means that their debugging configuration is separated from the applications configuration. The assembly should specify the RepositoryAttribute to set its logging repository.”

    Code

    I placed the following lines in the AssemblyInfo.cs file of my class library:

    // Log4Net configuration file location
    [assembly: log4net.Config.Repository("CompanyName.IntegrationLibName")]
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "CompanyName.IntegrationLibName.config", Watch = true)]
    

        

    References

    • LogManagerMembers
    • Jacob’s Answer
    • Jeroen’s Answer
    • McWafflestix’s Answer
    • log4net Manual – Repositories
    • log4NET from a class library (dll)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • 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 Yes, it does. jQuery is just a wrapper for Javascript… May 12, 2026 at 12:38 am
  • Editorial Team
    Editorial Team added an answer There are spring examples, PetClinic and JPetStore. In these links… May 12, 2026 at 12:38 am
  • Editorial Team
    Editorial Team added an answer @tialaramex: It's true that Microsoft massively reworked the decompression code… May 12, 2026 at 12:38 am

Related Questions

I actually have two questions regarding the same problem but I think it is
I'm writing a microformats parser in C# and am looking for some refactoring advice.
mod1.py import mod2 class Universe: def __init__(self): pass def answer(self): return 42 u =
Background I am trying to create a copy of a business object I have

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.