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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:28:39+00:00 2026-05-31T13:28:39+00:00

I want a NLog target to stop listening to the log. The RemoveTarget method

  • 0

I want a NLog target to stop listening to the log. The RemoveTarget method doesn’t seem to work. Here is a failing test.

public class when_stopping_to_listen
{
    static Logger Logger;
    static MemoryTarget target;

    Establish context = () =>
    {
        var config = new LoggingConfiguration();
        Logger = LogManager.GetLogger("TestLogger");

        target = new MemoryTarget {Layout = "${message}", Name = "TestTarget"};

        config.AddTarget(target.Name, target);
        config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, target));

        LogManager.Configuration = config;
    };

    Because of = () =>
    {
        var config = LogManager.Configuration;
        config.RemoveTarget(target.Name);
        LogManager.Configuration = config;
        Logger.Info("Test");
    };

    It should_be_empty = () => target.Logs.ShouldBeEmpty();
}

Thanks in advance.

  • 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-31T13:28:40+00:00Added an answer on May 31, 2026 at 1:28 pm

    I don’t know why RemoveTarget doesn’t work. But if you remove the target from each rule the test passes:

    Because of = () =>
    {
        foreach (var rule in config.LoggingRules)
        {
            rule.Targets.Remove(target);
        }        
        Logger.Info("Test");
    };
    

    And if you remove the LoggingRule instead of the target it also works:

    public class when_stopping_to_listen
    {
        //...
        static LoggingRule rule;
    
        Establish context = () =>
        {
            //...
            rule = new LoggingRule("*", LogLevel.Trace, target);
            config.LoggingRules.Add(rule);    
            LogManager.Configuration = config;
        };
    
        Because of = () =>
        {
            var config = LogManager.Configuration;
            config.LoggingRules.Remove(rule);
            LogManager.Configuration = config;
            Logger.Info("Test");
        };      
    
        //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NLog's file target is pretty configurable , but it doesn't seem to support what
I am using NLog and I want to log to RichTextBox and File at
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
I am trying to log via the NLog MailTarget. It works just fine, but
I want to be able to read an NLog configuration file from disk using
I want to calculate O(n log(n)) without using the master theorem. Does anyone know
I'm setting up log4net and want to write debug messages in debug.log, info messages
How can I use RichTextBox Target in WPF application? I don't want to have
NLog allows me to use SplitGroup to log my messages to several targets. I'd
I am using NLog for the first time on a project. I want to

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.