JINTO JOSE
JINTO JOSE

Reputation: 89

How to collect application logs in windows phone 8.1?

I am new to windows phone platform.Is there anything available like logcat in android for windows for collecting logs?Thanks in advance.

Upvotes: 5

Views: 3486

Answers (2)

talkitbr
talkitbr

Reputation: 1142

You can use System.Diagnostics.Debug to view the logs on Visual Studio Console Window but you won't be able to collect them later because it's only shown during debug.

I recommend the use of MetroLog, a lightweight logging system designed specifically for Windows Store and Windows Phone apps.

You can install it using NuGet

Install-Package MetroLog

Here's an quick example:

using MetroLog;
using MetroLog.Targets;

LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new FileStreamingTarget());
GlobalCrashHandler.Configure();

ILogger log = LogManagerFactory.DefaultLogManager.GetLogger<MainPage>();

log.Trace("This is a trace message.");

You can find a tutorial explaining how to add it on your project at http://talkitbr.com/2015/06/11/adicionando-logs-em-universal-apps. Also there is an explanation regarding retrieving these logs.

Upvotes: 0

Decade Moon
Decade Moon

Reputation: 34286

Windows 8.1 introduced new classes to simplify logging. These classes are LoggingChannel, LoggingSession and others.

Here's an example:

App.xaml.cs

LoggingSession logSession;
LoggingChannel logChannel;

public App()
{
    this.InitializeComponent();
    this.UnhandledException += App_UnhandledException;
}

void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    logChannel.LogMessage("Unhandled exception: " + e.Message);
    logSession.SaveToFileAsync(Windows.Storage.ApplicationData.Current.LocalFolder, "MainLog.log").AsTask().Wait();
}

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    logSession = new LoggingSession("MainLogSession");
    Resources["MainLogSession"] = logSession;

    logChannel = new LoggingChannel("AppLogChannel");
    logSession.AddLoggingChannel(logChannel);
}

MainPage.xaml.cs

LoggingChannel logChannel;

public MainPage()
{
    this.InitializeComponent();

    var logSession = (LoggingSession)Application.Current.Resources["MainLogSession"];
    logChannel = new LoggingChannel("MainPageLogChannel");
    logSession.AddLoggingChannel(logChannel);
    logChannel.LogMessage("MainPage ctor", LoggingLevel.Information);
}

I highly recommend watching the Making your Windows Store Apps More Reliable keynote during the 2013 build conference, where Harry Pierson demonstrates these new APIs in more detail (including uploading the log file to a backend server using a background task that gets executed when the phone is connected to AC power).

Upvotes: 6

Related Questions