如何将LoggingChannel LogMessages记录到StorageFile?

时间:2014-11-25 05:42:42

标签: c# logging windows-store-apps error-logging storagefile

我想在我的Windows应用商店应用中添加例外记录。基于here的想法,我开始在App.xaml.cs中使用此代码:

密封部分类App:应用程序 {     私有LoggingChannel频道;     私有LoggingSession会话;

/// <summary>
/// Initializes the singleton application object.  This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;

    channel = new LoggingChannel("PlatypiChannel");
    session = new LoggingSession("PlatypiSession");
    session.AddLoggingChannel(channel, LoggingLevel.Error);

    UnhandledException += Application_UnhandledException;
}

async private void Application_UnhandledException(object sender, UnhandledExceptionEventArgs ex)
{
    ex.Handled = true;
    String exceptionCornucopia = String.Format("Message0 == {0}; Message1 == {1}; HResult == {2}; Inner Ex == {3}; StackTrace == {4}", ex.Message, ex.Exception.Message, ex.Exception.HResult, ex.Exception.InnerException, ex.Exception.StackTrace);
    channel.LogMessage(exceptionCornucopia, LoggingLevel.Error);
    // not seeing how this saves the channel's logged messages...???
    StorageFile logFile = await session.SaveToFileAsync(ApplicationData.Current.LocalFolder, "CrashLog");
}

正如评论所示,在我看来,最后一行只是保存一个名为&#34; CrashLog&#34;到LocalFolder。但LogMessages如何进入该文件?显然这里缺少一个关键部分。

1 个答案:

答案 0 :(得分:1)

我知道这个问题已经开放了很长时间,但我只想为其他人找到答案。

这里的秘密是 LogMessages 都被写入 LoggingChannel ,它本身之前已经在 LoggingSession 中注册了: p>

  

session.AddLoggingChannel(channel,LoggingLevel.Error);

当会话保存到文件后,它显然知道相关的频道以及搜索待处理日志消息的位置。