Warm tip: This article is reproduced from stackoverflow.com, please click
.net rabbitmq log4net

How do I prevent RabbitMQ from naming threads?

发布于 2020-04-08 23:37:02

At some point during our project upgrades, we started noticing that names were being assigned to our RabbitMq client threads. This is causing a lot of unwanted spam in our log files.

e.g. A typical log message went from this

2019-10-22 10:56:17,981 [7] INFO - Publishing 440d9474-7c96-4226-8023-ca086dc0e143.product.001 to 127.0.0.1:5672. 

to this:

2019-10-22 10:56:17,981 [WorkPool-Session#1:Connection(a0437bf5-25c9-44f7-9a66-d3173e692fb2,amqp:/127.0.0.1:5672)] INFO - Publishing 440d9474-7c96-4226-8023-ca086dc0e143.product.001 to 127.0.0.1:5672. 

What caused this, and is there any way to revert this behaviour to what it was previously? No logic was changed, but the projects were upgraded to .NET 472 and certain Nuget packages were also upgraded.

We're not interested in the RabbitMq client's thread name - we just care about the numeric thread ID.

EDIT:

Here's a small progam that demonstrates the issue. You need a RabbitMQ server to connect to with the relevant details. Messages will be logged when the client reads them from the queue:

    public static void Main(string[] args)
    {
        var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
        XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
        var logger = LogManager.GetLogger(Assembly.GetEntryAssembly(), "Console");

        var fact = new ConnectionFactory {HostName = "127.0.0.1", UserName = "guest", Password = "guest", Port = 5672 };
        var conn = fact.CreateConnection();

        var channel = conn.CreateModel();
        channel.ExchangeDeclare("demo", "topic");
        var queue = channel.QueueDeclare();
        channel.QueueBind(queue.QueueName, "demo", "*");
        var consumer = new EventingBasicConsumer(channel);

        consumer.Received += (sender, message) =>
        {
           logger.Info("Message Received!");
        };

        logger.Info("About to receive messages...");
        channel.BasicConsume(queue.QueueName, true, consumer);

        Console.ReadLine();
    }

log4net.config:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="Console" />
  </root>
  <appender name="Console" type="log4net.Appender.ConsoleAppender" >
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %c %-5level - %message%newline" />
    </layout>
  </appender>
</log4net>

Nuget Versions:

log4net 2.0.8

RabbitMQ.Client 5.1.2

Log output:

2020-01-31 16:13:43,556 [1] Console INFO - About to receive messages...

2020-01-31 16:13:43,807 [WorkPool-Session#1:Connection(aa8913fd-aec2-4bd8-9cdc-2efc4f484363,amqp://127.0.0.1:5672)] Console INFO - Message Received!

2020-01-31 16:13:43,807 [WorkPool-Session#1:Connection(aa8913fd-aec2-4bd8-9cdc-2efc4f484363,amqp://127.0.0.1:5672)] Console INFO - Message Received!

Notice how the Thread IDs from the RabbitMq Consumer are named. I would like to revert back to the previous behaviour (numeric thread IDs) if possible.

Questioner
ilitirit
Viewed
65
ilitirit 2020-02-01 05:26

I've found the offending code. Indeed, the observed behaviour was caused by upgrading our version of the RabbitMq Client to 5.x (this code is different on the master branch).

#if NETFX_CORE
            System.Threading.Tasks.Task.Factory.StartNew(Loop, System.Threading.Tasks.TaskCreationOptions.LongRunning);
#else
            var thread = new Thread(Loop)
            {
                Name = "WorkPool-" + name,
                IsBackground = true
            };
#endif

https://github.com/rabbitmq/rabbitmq-dotnet-client/blob/5.x/projects/client/RabbitMQ.Client/src/client/impl/ConsumerWorkService.cs

If you specify "threadid" in the PatternLayout for log4net, you will only get the thread ID if a thread name is not specified. To answer my own question, there doesn't appear to be a way to easily change this behaviour.