Are there any exception logging components in Windows Phone applications? - windows-phone-7

Are there any exception logging components in Windows Phone applications? Do you have any suggestions about logging records in Windows Phone applications?

For logging to Visual Studio while running, you can use Debug.Write.
As far as logging exceptions to a file, WP7Contrib has an exception logging library that is available as part of WP7Contrib.Core on NuGet. WP7Contrib.Core's only dependencies are the Rx libraries.
Edit: You can use the WP7Contrib logging library like this:
private ILogManager logManager = new LoggingService();
private void Application_Launching(object sender, LaunchingEventArgs e)
{
logManager.Enable();
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
logManager.Enable();
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
logManager.Disable();
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
logManager.Disable();
}
Edit 2:
Having said that, I'd probably just use an extention method:
// App.xaml.cs
private ILogManager logManager = new LoggingService();
public App()
{
logManager.Attach(PhoneApplicationService.Current);
}
// LogManagerExtensions.cs
public static class LogManagerExtensions
{
public static void Attach(this ILogManager logManager, PhoneApplicationService appService)
{
appService.Launching += (s,e) => logManager.Enable();
appService.Activated += (s,e) => logManager.Enable();
appService.Deactivated += (s,e) => logManager.Disable();
appService.Closing += (s,e) => logManager.Disable();
}
}

NLog is quite good
And a somewhat older
Silverlight and WP7 Exception Handling and Logging building blockk

I saw Telerik are talking about releasing RadDiagnostics soon. I'm planning to get a look at it in the next app I release. Details from http://blogs.telerik.com/blogs/posts/12-02-01/introducing-raddiagnostics-for-windows-phone.aspx

well.you can try WP7Contrib OpenSource Project At CodePlex[http://wp7contrib.codeplex.com/] and get Exception Log Record in the Windows phone Application.
if you did't want to use opensource Project 。 you can define Component by yourself. Store Exception record as file in Isolated Storage。

Related

How does Windows.System.Configuration.Install.Installer work

I am trying to configure my installer so that it will close any instances of the application before installation starts and relaunch the application once installation has finished.
The MS documents provide and example similar to the one below and indicate that the [RunInstaller(true)] annotation specifies whether the Visual Studio Custom Action Installer or the Installutil.exe (Installer Tool) should be invoked when the assembly is installed.
Of course this could mean just about anything...
I am using the Visual Studio Project Installer NuGet package to create the install files (setup.exe and APP.msi files). When configuring the Project Installer there is an option set configure the Custom Actions - I assume this is what the documentation means.
However the Custom Actions has options to add things to the Install, Commit sections - what has to be added to these sections and how does this relate to the Installer class defined below.
This whole area of installation seems to be poorly documented and there seem to be so many options, none of which seem to work properly.
You would think closing any running instances before installing an update and then relaunching after completing the installation would be a no brainer but it seems anything but simple.
Appreciate any assistance with a working example of how this can be achieved.
namespace FocusBracketer
{
[RunInstaller(true)]
public partial class Installer: System.Configuration.Install.Installer
{
public Installer() : base()
{
InitializeComponent();
// Attach the 'BeforeInstall' event.
this.BeforeInstall += new InstallEventHandler(Installer_BeforeInstall);
// Attach the 'Committed' event.
this.Committed += new InstallEventHandler(Installer_Committed);
// Attach the 'Committing' event.
this.Committing += new InstallEventHandler(Installer_Committing);
}
// Event handler for 'BeforeInstall' event.
private void Installer_BeforeInstall(object sender, InstallEventArgs e)
{
Console.WriteLine("");
Console.WriteLine("BeforeInstall Event occurred.");
Console.WriteLine("");
}
// Event handler for 'AfterInstall' event.
private void Installer_AfterInstall(object sender, InstallEventArgs e)
{
Console.WriteLine("");
Console.WriteLine("AfterInstall Event occurred.");
Console.WriteLine("");
System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(this.Context.Parameters["AssemblyPath"]) + #"\FocusBracketer.exe");
}
// Event handler for 'Committing' event.
private void Installer_Committing(object sender, InstallEventArgs e)
{
Console.WriteLine("");
Console.WriteLine("Committing Event occurred.");
Console.WriteLine("");
}
// Event handler for 'Committed' event.
private void Installer_Committed(object sender, InstallEventArgs e)
{
Console.WriteLine("");
Console.WriteLine("Committed Event occurred.");
Console.WriteLine("");
}
// Override the 'Install' method.
public override void Install(IDictionary savedState)
{
base.Install(savedState);
}
// Override the 'Commit' method.
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
}
// Override the 'Rollback' method.
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
public static void Main()
{
Console.WriteLine("Usage : installutil.exe Installer.exe ");
}
}
}

Call UWP service on Shared project

I have a Xamarin app where I implemented a service to do some printing. I create that service on UWP app because it needs some dependencies of it.
UWP Service:
public class PrintUWPService
{
PrintManager printmgr = PrintManager.GetForCurrentView();
PrintDocument PrintDoc;
PrintDocument printDoc;
PrintTask Task;
private Windows.UI.Xaml.Controls.WebView ViewToPrint = new Windows.UI.Xaml.Controls.WebView();
public PrintUWPService()
{
printmgr.PrintTaskRequested += Printmgr_PrintTaskRequested;
}
public async void Print(WebView viewToPrint, string htmlSource)
{
ViewToPrint.NavigateToString(htmlSource);
if (PrintDoc != null)
{
printDoc.GetPreviewPage -= PrintDoc_GetPreviewPage;
printDoc.Paginate -= PrintDoc_Paginate;
printDoc.AddPages -= PrintDoc_AddPages;
}
printDoc = new PrintDocument();
try
{
printDoc.GetPreviewPage += PrintDoc_GetPreviewPage;
printDoc.Paginate += PrintDoc_Paginate;
printDoc.AddPages += PrintDoc_AddPages;
var showprint = await PrintManager.ShowPrintUIAsync();
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
PrintDoc = null;
GC.Collect();
}
private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
var deff = args.Request.GetDeferral();
Task = args.Request.CreatePrintTask("Grocery List", OnPrintTaskSourceRequested);
deff.Complete();
}
async void OnPrintTaskSourceRequested(PrintTaskSourceRequestedArgs args)
{
var def = args.GetDeferral();
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
args.SetSource(printDoc.DocumentSource);
});
def.Complete();
}
private void PrintDoc_AddPages(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(ViewToPrint);
printDoc.AddPagesComplete();
}
private void PrintDoc_Paginate(object sender, PaginateEventArgs e)
{
PrintTaskOptions opt = Task.Options;
printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void PrintDoc_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
printDoc.SetPreviewPage(e.PageNumber, ViewToPrint);
}
}
}
Then I create interface for it:
public interface IPrintUWPService
{
void Print(WebView viewToPrint, string htmlSource);
}
Now inside a class of my shared project I want to call this service as:
private readonly IPrintUWPService _printService = DependencyService.Get<IPrintUWPService>();
But it does not work. it says:
IPrintUWPService does not exist in the current context
So I try to access to UWP class as:
using MyCompany.ProjectName.App.UWP
But it says that "UWP" namespace does not exist. What am I doing wrong? is not possible call service on UWP project inside shared project? Regards
Call UWP DependencyService on Shared project
As #Jason mentioned in above comment, you need declare the interface in the Xamarin Forms project and implement it in the UWP client project.
And please note, after implementing the interface in each platform project, the platform implementations must be registered with the DependencyService, so that Xamarin.Forms can locate them at runtime. Use DependencyAttribute like the following.
[assembly: Dependency(typeof(UWP Client namesapce))]

Has anyone been able to see a crash attachment with Xamarin Forms and AppCenter.ms?

As far as I know I have followed exactly the instructions:
I have set everything up as suggested. Used my secret key, enabled crashes. Had the set up checked by another developer and see the crash happened in appcenter.ms but still I never see any attached information.
Here's an example:
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
try
{
UIApplication.Main(args, null, "AppDelegate");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Crashes.TrackError(ex,
new Dictionary<string, string> {
{"Main", "Exception"},
{"Device Model", DeviceInfo.Model },
});
throw;
}
}
}
No matter what, when and how my application crashes I still will not get the attached information.
I am wondering has anyone got the attached data for crashes to work with XF ?
We can use AppCenter only after it has been started which according to official documentation on iOS we do it in AppDelegate class in the method FinishedLaunching. But the point is the class Application in Main.cs file is called before AppDelegate class.
If you want to see the attached info then you can try it for example in a XAML code-behind file by manually throwing an exception. Here is an example for a button's click event:
private void TheButton1_OnClicked(object sender, EventArgs e)
{
try
{
throw new DivideByZeroException("Testing attached info!");
}
catch (Exception exception)
{
Crashes.TrackError(exception,
new Dictionary<string, string> {{"Device Model", "My device model" }});
}
}
The attached info on TrackError() method i.e properties dictionary works on both Android and iOS. To see that info you need to go through this in App Center's panel:
From left panel choose Diagnostics.
From Groups section choose your specific group.
From tabs in top section choose Reports.
Choose your specific device.
The attached info is In Stacktrace tab and in Error properties section.
Just to correct, the additional data you attach with exception in TrackError method are mostly in catch blocks or generated exception in TrackError methods, so it will only displayed with those manually logged(TrackError) exceptions.
Crashes are exceptions that are not handled and logged automatically by appcenter so if you look in crash reports there will not be any attached data available.
Additional data sent with exception as properties can be found in reports section of error on appcenter.
I am sure you have initialized Crash service in OnStart method of App.xaml.cs class with correct app secrets and required platforms(android/ios).
I was able to track the crashes. The only difference is am tracking it from the native projects.
For Android in the MainActivity:
protected override void OnCreate(Bundle savedInstanceState)
{
...
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser;
...
}
private void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
{
var newExc = new Exception("UnhandledExceptionRaiser", e.Exception as Exception);
LogUnhandledException(newExc);
}
private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
{
var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
LogUnhandledException(newExc);
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
LogUnhandledException(newExc);
}
internal static void LogUnhandledException(Exception exception)
{
try
{
Crashes.TrackError(exception);
...
}
catch (Exception ex)
{
// just suppress any error logging exceptions
}
}
For iOS in the AppDelegate:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
...
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
...
}
private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
{
var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
LogUnhandledException(newExc);
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
LogUnhandledException(newExc);
}
internal static void LogUnhandledException(Exception exception)
{
try
{
...
}
catch
{
// just suppress any error logging exceptions
}
}

Sterling database not persist on Windows phone

I followed sterling database examples from several persons. Neither of them seems to work out for me. When I persist some stuff on my database everything clearly gets persisted using sterling (on my phone, not emulator) when debugging. However when I relaunch my app the database is empty. Is somebody else experiencing the same problem. Or does someone have a complete working example. I know my serializing and saving works... As long as I don't relaunch my app loading my state works...
Code in my app.cs
public static ISterlingDatabaseInstance Database { get; private set; }
private static SterlingEngine _engine;
private static SterlingDefaultLogger _logger;
private void Application_Launching(object sender, LaunchingEventArgs e)
{
ActivateEngine();
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
ActivateEngine();
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
DeactivateEngine();
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
DeactivateEngine();
}
private void ActivateEngine()
{
_engine = new SterlingEngine();
_logger = new SterlingDefaultLogger(SterlingLogLevel.Information);
_engine.Activate();
Database = _engine.SterlingDatabase.RegisterDatabase<SokobanDb>();
}
private void DeactivateEngine()
{
_logger.Detach();
_engine.Dispose();
Database = null;
_engine = null;
}
Code in my viewModel
public void LoadState(int level)
{
var levelState = App.Database.Load<LevelState>(level);
if (levelState != null)
{
//TODO: check if game started, then create board from boardstring property else create new board
//Labyrint = new Labyrint(Factory.CreateBoard());
NewGame(level);
}
else
{
NewGame(level);
}
}
public void SaveState()
{
var levelState = new LevelState { LevelId = _level, Moves = Labyrint.Moves, Board = Labyrint.ToString() };
App.Database.Save(levelState);
App.Database.Flush(); //Required to clean indexes etc.
}
The default Sterling database uses an in-memory driver. To persist, pass it an isolated storage driver. Per the documentation guide quickstart:
https://sites.google.com/site/sterlingdatabase/sterling-user-guide/getting-started
The code looks like this:
_databaseInstance = _engine.SterlingDatabase.RegisterDatabase(new IsolatedStorageDriver());
Note the instance of the isolated storage driver being passed in. That should do it for you.
When in doubt, take a look at the unit tests shipped with the source. Those contain tons of examples of memory, isolated storage, etc. to show various patterns for setting it up.

How to customize the method templates in Visual Studios

Is there a way to customize/edit the default template of the event handling methods in Visual Studios 2008/2010 such that upon creation, the event handler already has a try/catch block in it?
the default behavior is
private void button1_Click(object sender, EventArgs e)
{
}
I'd like to be able to set the development environment to do
private void button1_Click(object sender, EventArgs e)
{
try
{
///TODO: Add logic here
}
catch
{
throw;
}
finally
{
}
}
I've already edited the templates located in the ItemTemplates folder so that they meet the coding standard where I work, but I'd also like to edit the method templates if possible. I've searched the 'Net for the better part of a week and found nothing. Any assistance would be greatly appreciated.
![Snippet Manager1
Have you considered Editing Event method stub in the Code Snippet manager or adding a new snippet?

Resources