2

I am trying to send a message to MSMQ queue using Log4Net.I spent lot of time to understand the functionality from the net. But I couldn't find much help. The following is my source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;

namespace MQTest
{
    class Program
    {   
    private static readonly ILog ilogger = LogManager.GetLogger("");
    static void Main(string[] args)
    {
        ilogger.Debug("This should go to the queue");
        ilogger.Fatal("This should go to the queue");
    }
    }
}

app.config

<log4net>
<appender name="MsmqAppender" type="MQTest.Appender.MsmqAppender, MQTest">
<queueName value="private$\test_queue"/>
<labelLayout value="LOG [%level] %date"/>
<layout type="log4net.Layout.XmlLayoutSchemaLog4j"/>
</appender>
<root>
  <level value="ALL"/>
   <appender-ref ref="MsmqAppender" />
</root>
</log4net>

When I debug the code, I have noticed that IsDebugEnabled = false, IsFatalEnabled =false etc. I don't know how to populate the above flags, while using MsmqAppender.

Thanks for your help

Ullan
  • 1,311
  • 8
  • 21
  • 34

1 Answers1

0

You haven't got it set up right.

You've defined your appender and registered it in the config file, so when you want to log something you just use the ILog logger you get from the log manager, you don't need to create an instance of logMSMQ:

myLogger.Debug("This should go to the queue");

Looking at the appender and config, a couple of things stand out:

  1. You should not be calling XmlConfigurator.Configure(); from within the appender

  2. You're not registering the appender in your config correctly: you need to specify it by type and assembly namespace, e.g.: <appender name="MsmqAppender" type="AssemblyNamespace.MSMQTest.LogMSMQ, AssemblyNamespace">

  3. You have not specified it by name in the root logger configuration:

<root>
  <level value="ALL" />
  <appender-ref ref="MsmqAppender" />
</root>

While you're getting it set up, turn on log4net internal debugging as it will help you see what's going on.

Community
  • 1
  • 1
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • I have added the queue config, but still your code is not working – Ullan Nov 17 '14 at 16:02
  • What kind of working are we looking at? Can you update the code in the question and give details of what's not working? – stuartd Nov 17 '14 at 16:09