如何接收由 BinaryLogFormatter 格式化的消息

发布于 2024-11-03 18:42:27 字数 270 浏览 2 评论 0原文

我正在使用 MS 模式和实践企业库 MsmqTraceListener 使用 BinaryLogFormatter 将条目记录到专用队列。

我现在想从队列中读取这些日志条目。我需要先设置 MessageQueue 的 Formatter 属性,然后才能查看 message.Body。我希望能够使用 EnterpriseLibrary 的 BinaryLogFormatter,但无法将其转换为 IMessageFormatter。 (InvalidCastException)

我错过了什么?

I'm using the MS Patterns and Practices Enterprise Library MsmqTraceListener to log entries to a private queue using the BinaryLogFormatter.

I would now like to read those log entries off the queue. I need to set the Formatter property of the MessageQueue before I can look at the message.Body. I expected to be able to use the BinaryLogFormatter of the EnterpriseLibrary, but I can't cast that as an IMessageFormatter. (InvalidCastException)

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执妄 2024-11-10 18:42:27

您是否查看过企业库分发服务?它可能可以开箱即用地执行您想要的操作。实施简短指南:使用 MSMQ 进行企业库 5 日志记录

如果您不想使用完整的分发服务器服务,请查看源代码以了解它们如何访问队列。看起来他们直接使用 BinaryLogFormatter 进行反序列化。来自 MsmqLogDistributor.cs

using (MessageQueue msmq = CreateMessageQueue())
{
    Message message = msmq.Peek();

    string serializedEntry = message.Body.ToString();

    LogEntry logEntry = null;
    try
    {
        logEntry = BinaryLogFormatter.Deserialize(serializedEntry);
    }
    catch (FormatException formatException)
    {
        ...
    }
    catch (SerializationException serializationException)
    {
        ...
    }

    if (logEntry != null)
    {
        logWriter.Write(logEntry);
    }

    message = msmq.Receive();

    if (this.StopReceiving)
    {
        this.isCompleted = true;
        return;
    }
}

Have you looked at the Enterprise Library Distributor Service? It might do what you want out of the box. A short guide to implement: Enterprise Library 5 Logging using MSMQ.

If you don't want to use the full Distributor Service look at the source code to see how they access the queue. It looks like they are deserializing using the BinaryLogFormatter directly. From MsmqLogDistributor.cs:

using (MessageQueue msmq = CreateMessageQueue())
{
    Message message = msmq.Peek();

    string serializedEntry = message.Body.ToString();

    LogEntry logEntry = null;
    try
    {
        logEntry = BinaryLogFormatter.Deserialize(serializedEntry);
    }
    catch (FormatException formatException)
    {
        ...
    }
    catch (SerializationException serializationException)
    {
        ...
    }

    if (logEntry != null)
    {
        logWriter.Write(logEntry);
    }

    message = msmq.Receive();

    if (this.StopReceiving)
    {
        this.isCompleted = true;
        return;
    }
}
背叛残局 2024-11-10 18:42:27

需要确保做的一件事是在设置消息队列时包含以下内容。如果不是,反序列化将不起作用。

((XmlMessageFormatter)messageQueue.Formatter).TargetTypeNames = new string[] { "System.String" };

花了一个下午的时间试图解决这个问题。

One thing to make sure to do is to include the following when setting up your messageQueue. If not the deserilizing will not work.

((XmlMessageFormatter)messageQueue.Formatter).TargetTypeNames = new string[] { "System.String" };

One waisted afternoon trying figure this one out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文