Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture MVXTrace in error reporting tool

I'm using MvvmCross and playing around with some ways to get the MvxTrace in my reporting tool. In this case I'm using Raygun. Raygun gives me the option of including additional messages to the error message that I want to throw, which is what I'm thinking I have to use to get this to happen. Basically I want to do something like this in the code:

var client = new RaygunClient();
var tags = new List<string> { "myTag" };
var customData = new Dictionary<int, string>() { {1, "**MVXTrace stuff here**"} };
client.Send(exception, tags, customData);

How can I hook this up? I'm getting confused when I'm looking at the Trace setup. I'm assuming I need to do something with my DebugTrace file that I'm using to inject. Right now it looks like this:

public class DebugTrace : IMvxTrace
{
    public void Trace(MvxTraceLevel level, string tag, Func<string> message)
    {
        Debug.WriteLine(tag + ":" + level + ":" + message());
    }

    public void Trace(MvxTraceLevel level, string tag, string message)
    {
        Debug.WriteLine(tag + ":" + level + ":" + message);
    }

    public void Trace(MvxTraceLevel level, string tag, string message, params object[] args)
    {
        try
        {
            Debug.WriteLine(string.Format(tag + ":" + level + ":" + message, args));
        }
        catch (FormatException)
        {
            Trace(MvxTraceLevel.Error, tag, "Exception during trace of {0} {1} {2}", level, message);
        }
    }
}

Can I do something that hooks into the IMvxTrace logic to attach inner exceptions and etc to my RaygunClient? It's hard for me to see what is causing specific errors because if I leave it the way it is I get errors that look like this:

[MvxException: Failed to construct and initialize ViewModel for type MyProject.Core.ViewModels.SignatureViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information]

Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel(Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState, IMvxViewModelLocator viewModelLocator):0

Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel(Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState):0

Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest(Cirrious.MvvmCross.ViewModels.MvxViewModelRequest viewModelRequest, IMvxBundle savedState):0

Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent(Android.Content.Intent intent, IMvxBundle savedState):0

Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load(Android.Content.Intent intent, IMvxBundle savedState, System.Type viewModelTypeHint):0

Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel(IMvxAndroidView androidView, IMvxBundle savedState):0

Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions+<>c__DisplayClass3.<OnViewCreate>b__1():0

Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate(IMvxView view, System.Func`1 viewModelLoader):0

Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate(IMvxAndroidView androidView, Android.OS.Bundle bundle):0

Cirrious.MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled(System.Object sender, Cirrious.CrossCore.Core.MvxValueEventArgs`1 eventArgs):0

(wrapper delegate-invoke) System.EventHandler`1<Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>>:invoke_void__this___object_TEventArgs (object,Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>)

Cirrious.CrossCore.Core.MvxDelegateExtensionMethods.Raise[Bundle](System.EventHandler`1 eventHandler, System.Object sender, Android.OS.Bundle value):0

Cirrious.CrossCore.Droid.Views.MvxEventSourceActivity.OnCreate(Android.OS.Bundle bundle):0

MyProject.Droid.Views.SignatureView.OnCreate(Android.OS.Bundle bundle):0

Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState):0

(wrapper dynamic-method) object:3af7783d-a44d-471c-84a6-662ebfaea4ae (intptr,intptr,intptr)

It would be really helpful, as that message suggests, if I could get the MvxTrace with it to track down exactly why initializing this ViewModel failed. Any suggestions?

like image 586
PkL728 Avatar asked Oct 21 '22 03:10

PkL728


1 Answers

This is how I do it on Android. I use the Android.Util.Log class. This will then log the messages to the Android Device Log.

public class DebugTrace : IMvxTrace
{
    public void Trace(MvxTraceLevel level, string tag, Func<string> message)
    {
        Trace(level, tag, message());
    }

    public void Trace(MvxTraceLevel level, string tag, string message)
    {
        switch (level)
        {
            case MvxTraceLevel.Diagnostic:
                Log.Debug(tag, message);
                break;
            case MvxTraceLevel.Warning:
                Log.Warn(tag, message);
                break;
            case MvxTraceLevel.Error:
                Log.Error(tag, message);
                break;
            default:
                Log.Info(tag, message);
                break;
        }
    }

    public void Trace(MvxTraceLevel level, string tag, string message, params object[] args)
    {
        try
        {
            Trace(level, tag, string.Format(message, args));
        }
        catch (FormatException)
        {
            Trace(MvxTraceLevel.Error, tag, "Exception during trace of {0} {1}", level, message);
        }
    }
}

You can then get the log using the following:

public class AndroidLogReader
{
    public string ReadLog(string tag)
    {
        var cmd = "logcat -d";
        if (!string.IsNullOrEmpty(tag)) cmd += " -s " + tag;

        var process = Java.Lang.Runtime.GetRuntime().Exec(cmd);
        using (var sr = new StreamReader(process.InputStream))
        {
            return sr.ReadToEnd();
        }
    }
}
like image 113
Kiliman Avatar answered Oct 23 '22 04:10

Kiliman