如何以编程方式清除MSMQ系统队列日志?

时间:2009-06-30 12:27:08

标签: .net msmq logging

需要批量每周清除系统队列日志。

3 个答案:

答案 0 :(得分:5)

系统队列的正确格式:

FormatName:Direct=os:.\\System$;JOURNAL

我在Windows 7和Windows 2003上测试了这种格式。

(os之后的点:表示本地主机/本地计算机)

var systemJournalQueue = new MessageQueue("FormatName:Direct=os:.\\System$;JOURNAL");
var systemDeadLetterQueue = new MessageQueue("FormatName:Direct=os:.\\System$;DEADLETTER");
var systemDeadXLetterQueue =new MessageQueue("FormatName:Direct=os:.\\System$;DEADXACT"));

systemJournalQueue.Purge();

或者如果你想保留N天的消息,你可以这样做:

private static void PurgeQueues(int archiveAfterHowManyDays, MessageQueue queue)
{
    queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(System.String) });
    queue.MessageReadPropertyFilter.ArrivedTime = true;

    using (MessageEnumerator messageReader = queue.GetMessageEnumerator2())
    {
        int counter = 0;
        while (messageReader.MoveNext())
        {
            Message m = messageReader.Current;
            if (m.ArrivedTime.AddDays(archiveAfterHowManyDays) < DateTime.Now)
            {
                queue.ReceiveById(m.Id);
                counter++;
            }
        }
    }
}

答案 1 :(得分:1)

MessageQueue mq = new MessageQueue(".\\Journal$");
mq.Purge();

MessageQueue mq = new MessageQueue(".\\myQueue\\Journal$");
mq.Purge();

如果它是您创建的队列。确保用户具有正确的权限。看到: http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.aspx

答案 2 :(得分:1)

Answerd here

MessageQueue mq = new MessageQueue("DIRECT=OS:computername\SYSTEM$;JOURNAL");
mq.Purge();