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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:10:19+00:00 2026-06-05T09:10:19+00:00

My goal is to create a log4net library that can be shared across multiple

  • 0

My goal is to create a log4net library that can be shared across multiple projects.

In my solution which is in .net 4.0, I created a class library called Logger and referenced it from web project.

Now I created a logger.config in the class library and put all the configuration in the logger.config file. I then used

[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile = "Logger.config")]

When I run the web app nothing is getting logged. So I added this line of code in web.config

<add key="log4net.Internal.Debug" value="true"/> 

which gave me debugging info and error information

Failed to find configuration section ‘log4net’ in the application’s .config file. Check your .config file for the and elements. The configuration section should look like:

I moved the configuration from logger.config to web.config and everything seems to work fine.

I don’t want the log4net configuration in web.config but have it logger.config as a cleaner approach. The goal is to make other projects use this library and not have to worry about configuration in every project.

Now the question is, How do I do this? What am I doing wrong? Any suggestion with code example will be beneficial to everyone.

FYI, I am using structure map IOC to reslove the logger before logging to it.

  • 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-05T09:10:21+00:00Added an answer on June 5, 2026 at 9:10 am

    I believe you are trying to reinvent the wheel, there are several tools to abstract the loggers from your code decoupling the logger framework from your code:

    http://slf.codeplex.com/

    http://netcommon.sourceforge.net/

    Those are the most common in .Net applications.

    Well now if you insist in using your own abstraction (which is valid), this is my suggestion:

    First of all, I assume that you would have something like:

    interface ILogger
    {
        void Debug(string message);
        void Info(string message);
        void Warn(string message);
        …..
    }
    

    Ok so in your code you would inject a singleton reference of your logger, like:

    x.For<ILogger>().Singleton().Use<MyLogger>();
    

    Now, I usually prefer convention over configuration, but in the case of loggers, the configuration through a config file is a big advantage because when your code is in production, you will want sometimes to change the logging rules by disabling or enabling loggers, change the log-level, and enable/disable named loggers.

    The easiest and most reusable way I have found to accomplish this is by placing the configuration in an external file as you commented, then I do not bother calling the XmlConfigurator in code, because it is simpler to me just add a couple of application settings to my config file like this

    <!-- Relative to the application root path -->
    <add key="log4net.Config" value="Logger.log4net"/>
    <add key="log4net.Config.Watch" value="True"/>
    

    That’s it. Those application settings override your log4net configuration and they are easy to maintain. Your config file will look like:

    <?xml version="1.0" encoding="utf-8" ?>
    
    <!-- This section contains the log4net configuration settings -->
    <log4net>
    
      <!-- Define some output appenders -->
    
      <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
        <file value="webapp-log.txt" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
        </layout>
      </appender>
    
      <!-- Setup the root category, add the appenders and set the default priority -->
    
      <root>
        <level value="DEBUG" />
        <appender-ref ref="LogFileAppender" />
      </root>
    
    </log4net>
    

    To configure more appenders: click here

    In your projects you only need a reference to your logger abstraction component, and just in the web project (or windows application, it’s the same) add the log4net config file and just those application settings

    Since you are creating a reusable component I would recommend you to setup an internal Nuget server in your organization (it’s just an IIS application) and use it to deploy your components, in this case the library containing the logger. Also it would be better if you create a different Nuget package containing only the application settings and the log4net configuration file, so you can install this Nuget package only in your web project

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

Sidebar

Related Questions

My goal is to create a trait that a case class can extend, which
The goal is to create a mock class which behaves like a db resultset.
Can't get this to work. Goal is to create a overflowing scrollable div that
My goal is to create some kind of swing component that can do two
My goal is to create a map of maps so that I can retrieve
High Level Goal: Create a single Maven Web Application project that can be used
The goal: To create a .NET dll i can reference from inside SQL Server
My goal is to create an eBook that I can read with the Mobipocket
I've created an object Chartblock that implements QGraphicsItem. My goal is to create a
The goal is to create a program which will effectively let the user create

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.