2014-11-25 44 views
0

我想添加例外记录到我的Windows应用商店。如何将LoggingChannel LogMessages记录到StorageFile?

密封部分类应用程序:基于从here一个想法,我已经与这个码在App.xaml.cs开始时应用 { 私人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"); 
} 

正如评论指出的,在我看来,最后一行简单地节省了一个名为“系统崩溃日志”到LocalFolder文件。但LogMessages如何进入该文件?显然这里缺少一个关键部分。

回答

1

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

这里的秘密是,日志消息都写进LoggingChannel,这本身已预先登记的LoggingSession

session.AddLoggingChannel(通道,LoggingLevel.Error) ;

当会话被保存到一个文件时,它显然知道关联的通道以及在哪里搜索待处理的日志消息。