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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:20:43+00:00 2026-05-13T00:20:43+00:00

I’ve previously used log4net, but my current employer uses Enterprise Library application blocks. I

  • 0

I’ve previously used log4net, but my current employer uses Enterprise Library application blocks. I had previously developed unit tests for my core logging classes as follows and was wondering if someone knew the equivalent for the OneTimeSetup code below for the logging app block (sorry for the long code post):

public abstract class DataGathererBase
{
  public readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  public void CollectData()
  {
    this.LogDebug("Initialize started");
  }

  public static class Logger
  {
    private static LoggingSettings settings = LoggingSettings.GetLoggingSettings(new SystemConfigurationSource());

    static Logger()
    {
      log4net.Config.XmlConfigurator.Configure();
    }

    public static void LogDebug(this DataGathererBase current, string message)
    {
      if (current.logger.IsDebugEnabled)
      {
        current.logger.Debug(string.Format("{0} logged: {1}", current.GetType().Name, message));
      }
    }
  }

[TestFixture]
public class LoggerTests:DataGathererBase
{
  private ListAppender appender;
  private static ILog log;

  [TestFixtureSetUp]
  public void OneTimeSetup()
  {
    appender = new ListAppender();
    appender.Layout = new log4net.Layout.SimpleLayout();
    appender.Threshold = log4net.Core.Level.Fatal;
    log4net.Config.BasicConfigurator.Configure(appender);
    log = LogManager.GetLogger(typeof(ListAppender));
  }

  [Test]
  public void TestLogging()
  {
    this.LogDebug("Debug");
    Assert.AreEqual(0, ListAppender.logTable.Count());
  }
}
  • 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-13T00:20:44+00:00Added an answer on May 13, 2026 at 12:20 am

    To give credit, this answer is based on a David Hayden article which is based on an Alois Kraus article, Programatic Configuraton – Enterprise Library (v2.0) Logging Block . Read those two articles for a good look at programmatic access to Enterprise Library logging.

    I wasn’t familiar with ListAppender so I created a CustomTraceListener that sticks the log messages in a List<string>:

      public class ListAppender : Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener
      {
        private List<string> list = new List<string>();
    
        public override void Write(string message)
        {
        }
    
        public override void WriteLine(string message)
        {
          list.Add(message);
        }
    
        public List<string> LogTable
        {
          get
          {
            return list;
          }
        }
      }
    

    Here is a modified LoggerTests class that programmatically accesses the EL logging classes to setup the tests (this does not use NUnit):

      public class LoggerTests 
      {
        private ListAppender appender;
        private static LogWriter log;
    
        public void OneTimeSetup()
        {
          appender = new ListAppender();
    
          // Log all source levels
          LogSource mainLogSource = new LogSource("MainLogSource", SourceLevels.All);
          mainLogSource.Listeners.Add(appender);
        
          // All messages with a category of "Error" should be distributed
          // to all TraceListeners in mainLogSource.
          IDictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
          traceSources.Add("Error", mainLogSource);
    
          LogSource nonExistentLogSource = null;    
          log = new LogWriter(new ILogFilter[0], traceSources, nonExistentLogSource,
                            nonExistentLogSource, mainLogSource, "Error", false, false);
        }
    
        public void TestLogging()
        {
          LogEntry le = new LogEntry() { Message = "Test", Severity = TraceEventType.Information };
          le.Categories.Add("Debug");
          log.Write(le);
    
          // we are not setup to log debug messages
          System.Diagnostics.Debug.Assert(appender.LogTable.Count == 0);
    
          le.Categories.Add("Error");
          log.Write(le);
          
          // we should have logged an error
          System.Diagnostics.Debug.Assert(appender.LogTable.Count == 1);
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I have a jquery bug and I've been looking for hours now, I can'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.