【问题标题】:Enterprise Library Silverlight Integration Pack Logging app block remote trace listener can't send out log企业库 Silverlight 集成包日志记录应用程序阻止远程跟踪侦听器无法发送日志
【发布时间】:2011-12-05 08:28:11
【问题描述】:

当发生未处理的异常时,全局未处理的异常处理程序中会弹出一个错误窗口,在此窗口中,有一个名为 Restart 的按钮,当单击此按钮时,我尝试将错误记录到远程跟踪侦听器以远程服务器,并重新启动应用程序,但事实证明,在应用程序重新启动后日志从未成功发送,我确保执行了日志语句,但似乎直到按钮才发送日志单击事件处理程序执行完成。

而且当我把发送日志和重启分成两个按钮时,效果很好。

我也尝试将任一部分放在另一个线程中,但它仍然不起作用。

我已经拔了几天头发了,有人能救我吗?非常感谢...

【问题讨论】:

标签: logging enterprise-library


【解决方案1】:

当您在使用 RemoteServiceTraceListener 时调用 Write 时,LogEntry 将被放置在要发送到服务器的列表中。每隔一段时间就会运行一个计时器并将列表中的项目发送到服务器。即使 SendImmediately 设置为 True,也会发生这种情况。如果 SendImmediately 设置为 True,则 Timer Callback 将在大约 1 秒后触发。

因此,发生的情况是,当您执行重新启动时,您正在拆除应用程序,然后它才能将消息发送到服务器。我曾希望将消息存储在独立存储中,然后在重新启动后发送回服务器,但这似乎没有发生。

我能够让它按照我认为你想要的方式工作,但它有点混乱——也许有更简单的方法?

首先创建一个 LoggingServiceFactory 的实例,然后设置一个回调来导航到应用 URL。

public static class LogHelper
{
    public static void Write(LogEntry logEntry)
    {
        // Factory is not configured so set config name
        var factory = EnterpriseLibraryContainer.Current.GetInstance<LoggingServiceFactory>();
        factory.EndpointConfigurationName = "CustomBinding_ILoggingService";

        var proxy = factory.CreateChannel();

        proxy.Write(logEntry, () =>
        {
            // Set callback Action
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(
                                    new Uri("http://localhost/app.aspx"));
            }); 
        });
    }
}

public static class ILoggingServiceExtension
{
    public static void Write(this ILoggingService proxy, LogEntry logEntry, Action action)
    {
        LogEntryMessage[] messages = new LogEntryMessage[] { logEntry.ToLogEntryMessage() };

        proxy.BeginAdd(messages, r =>
            {
                try
                {
                    proxy.EndAdd(r);
                    if (action != null)
                        action();
                }
                finally
                {
                    if (proxy != null)
                    {
                        ((IDisposable)proxy).Dispose();
                    }
                }
            }, null);
    }
}

然后通过LogHelper写入消息:

var logEntry = new LogEntry() { Message = "Test", Categories = { "default"} };            

LogHelper.Write(logEntry);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2013-08-01
    • 2011-03-27
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多