locked
error logging question

    Question

  • Hi,

    I want to enable logging in my win store app...I have seen the below MS example but as far as I can see from the example the error logging must be called in every possible error scenario using this code - MetroEventSource.Log.Info("Current Window is activating");

    I am looking for a way to log an error anytime my app falls over and closes...without having lots of try/catchs with the above line in the catch.  Is there a way to do this?  Some sort of generic logging that allows me to log anytime there is an error in the app where it will log the message of the error and possible the xaml page it occurred on..

    Thanks for any help...

    https://code.msdn.microsoft.com/windowsapps/Logging-Sample-for-Windows-0b9dffd7

    Wednesday, November 26, 2014 4:25 PM

Answers

  • Hi,

    I suggest to go with 

    LoggingChannel.LogMessage(String) 

    I have made a sample here for that, check if it fits your requirement

    https://code.msdn.microsoft.com/windowsapps/Logging-Sample-for-Windows-ecd3622f


    -Malleswar

    Wednesday, November 26, 2014 7:49 PM
  • I also suggest you go with the ETL logging... (the etw logging was the only way on win 8 but on win 8.1 you can actually use the logging channel and the session).

    And for the unhandled errors or unexpected crashes, you can always resort to UnhandledException event:

    App.Current.UnhandledException += (sender, args) => {
        channel.LogMessage(args.Exception.Message, LoggingLevel.Critical);
        //code to save log and background upload omitted
    };
    

    hope it helps...


    Can Bilgin
    Blog Samples CompuSight

    Wednesday, November 26, 2014 8:33 PM
  • If you are running a BackGround worker is possible to handle errors generically:

    using Microsoft.Phone.Controls;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace SL_BackgroundWorker_CS
    {
        public partial class Page : PhoneApplicationPage
        {
            private BackgroundWorker bw = new BackgroundWorker();
    
            public Page()
            {
                InitializeComponent();
    
                bw.WorkerReportsProgress = true;
                bw.WorkerSupportsCancellation = true;
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            }
            private void buttonStart_Click(object sender, RoutedEventArgs e)
            {
                if (bw.IsBusy != true)
                {
                    bw.RunWorkerAsync();
                }
            }
            private void buttonCancel_Click(object sender, RoutedEventArgs e)
            {
                if (bw.WorkerSupportsCancellation == true)
                {
                    bw.CancelAsync();
                }
            }
            private void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
    
                for (int i = 1; i <= 10; i++)
                {
                    if ((worker.CancellationPending == true))
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        // Perform a time consuming operation and report progress.
                        System.Threading.Thread.Sleep(500);
                        worker.ReportProgress(i * 10);
                    }
                }
            }
            private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled == true)
                {
                    this.tbProgress.Text = "Canceled!";
                }
    
                else if (!(e.Error == null))
                {
                    this.tbProgress.Text = ("Error: " + e.Error.Message);
                }
    
                else
                {
                    this.tbProgress.Text = "Done!";
                }
            }
            private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
            }
        }
    }

    http://msdn.microsoft.com/en-us/library/windows/apps/cc221403%28v=vs.105%29.aspx

    But I don't know about any support about an error event for your entire application on Windows Phone API.

    • Marked as answer by Ireland75 Thursday, November 27, 2014 10:55 AM
    Wednesday, November 26, 2014 5:08 PM
    • Marked as answer by Ireland75 Thursday, November 27, 2014 10:48 AM
    Wednesday, November 26, 2014 10:32 PM
    Moderator

All replies

  • If you are running a BackGround worker is possible to handle errors generically:

    using Microsoft.Phone.Controls;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace SL_BackgroundWorker_CS
    {
        public partial class Page : PhoneApplicationPage
        {
            private BackgroundWorker bw = new BackgroundWorker();
    
            public Page()
            {
                InitializeComponent();
    
                bw.WorkerReportsProgress = true;
                bw.WorkerSupportsCancellation = true;
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            }
            private void buttonStart_Click(object sender, RoutedEventArgs e)
            {
                if (bw.IsBusy != true)
                {
                    bw.RunWorkerAsync();
                }
            }
            private void buttonCancel_Click(object sender, RoutedEventArgs e)
            {
                if (bw.WorkerSupportsCancellation == true)
                {
                    bw.CancelAsync();
                }
            }
            private void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
    
                for (int i = 1; i <= 10; i++)
                {
                    if ((worker.CancellationPending == true))
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        // Perform a time consuming operation and report progress.
                        System.Threading.Thread.Sleep(500);
                        worker.ReportProgress(i * 10);
                    }
                }
            }
            private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled == true)
                {
                    this.tbProgress.Text = "Canceled!";
                }
    
                else if (!(e.Error == null))
                {
                    this.tbProgress.Text = ("Error: " + e.Error.Message);
                }
    
                else
                {
                    this.tbProgress.Text = "Done!";
                }
            }
            private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
            }
        }
    }

    http://msdn.microsoft.com/en-us/library/windows/apps/cc221403%28v=vs.105%29.aspx

    But I don't know about any support about an error event for your entire application on Windows Phone API.

    • Marked as answer by Ireland75 Thursday, November 27, 2014 10:55 AM
    Wednesday, November 26, 2014 5:08 PM
  • Hi,

    I suggest to go with 

    LoggingChannel.LogMessage(String) 

    I have made a sample here for that, check if it fits your requirement

    https://code.msdn.microsoft.com/windowsapps/Logging-Sample-for-Windows-ecd3622f


    -Malleswar

    Wednesday, November 26, 2014 7:49 PM
  • I also suggest you go with the ETL logging... (the etw logging was the only way on win 8 but on win 8.1 you can actually use the logging channel and the session).

    And for the unhandled errors or unexpected crashes, you can always resort to UnhandledException event:

    App.Current.UnhandledException += (sender, args) => {
        channel.LogMessage(args.Exception.Message, LoggingLevel.Critical);
        //code to save log and background upload omitted
    };
    

    hope it helps...


    Can Bilgin
    Blog Samples CompuSight

    Wednesday, November 26, 2014 8:33 PM
    • Marked as answer by Ireland75 Thursday, November 27, 2014 10:48 AM
    Wednesday, November 26, 2014 10:32 PM
    Moderator
  • Hi guys...thanks for all that help...great stuff!

    The unhandled exception is definitely a great help as is the code to simplify the logging.

    Thanks! for that

    Thursday, November 27, 2014 10:55 AM