5

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?

Matteo
  • 37,680
  • 11
  • 100
  • 115
PkL728
  • 955
  • 8
  • 21

2 Answers2

2

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();
        }
    }
}
Kiliman
  • 18,460
  • 3
  • 39
  • 38
  • This is how you would do it in production? At the point I'm getting my Raygun error messages, this app is out in "Release" mode. Your above code is giving me some ideas but want to verify you are using this for production code. – PkL728 Aug 05 '14 at 15:41
  • Yes, I have it in production. MvvmCross does output a lot of noise in the logs, but it's still helpful for tracking down issues. There are places where MvvmCross swallows exceptions (but logs them) and makes it difficult to trace otherwise. – Kiliman Aug 05 '14 at 19:16
  • Yea those are the types of exceptions I am trying to log. Could you give me an example of how you use ReadLog? Do you do something like: logReader.ReadLog(MvxTraceLevel.Error.ToString())? – PkL728 Aug 05 '14 at 19:28
  • var log = logReader.ReadLog(tag) where tag = "mvx" to get the MvvmCross logs. If you haven't already, I would suggest you open the Android Device Logging window. That way you can actually see what is being logged. You can filter by tag. – Kiliman Aug 05 '14 at 20:25
  • 1
    You can also specify the trace level like TAG:Level (e.g. "mvx:E" to get only error logs for tag mvx). – Kiliman Aug 05 '14 at 20:31
  • Fantastic. I was able to take what you showed me above and put together a solution here that works for me. I can't mark yours as the answer as your AndroidLogReader is just the first part. Showing the full solution below.... although if you have something better than mine I'll gladly accept yours as the answer! – PkL728 Aug 05 '14 at 20:36
1

Here is what I did to get this to work for me:

I have a BaseView that I'm using for all of my Android activities. I make use of this BaseView to hook up and log Unhandled Exceptions like so:

    public abstract class BaseView : MvxActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
        AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;
    }

    protected void HandleUnhandledException(object sender, UnhandledExceptionEventArgs args)
    {
        var e = (Exception)args.ExceptionObject;
        Mvx.Trace(MvxTraceLevel.Error, "Exception: {0}", e.ToLongString());
        var logReader = new AndroidLogReader();
        var logMessages = logReader.ReadLog("mvx:E");
        var customData = new Dictionary<string, string> { { "logMessage", logMessages } };
        var session = SessionController.Instance;
        var user = new RaygunIdentifierMessage(session.UserName + " " + session.Company);
        var rayMessage = RaygunMessageBuilder.New
            .SetEnvironmentDetails()
            .SetMachineName(Environment.MachineName)
            .SetClientDetails()
            .SetExceptionDetails(e)
            .SetUser(user)
            .SetUserCustomData(customData)
            .Build();
        RaygunClient.Current.Send(rayMessage);
    }

    protected void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
    {
        var exception = e.Exception;
        Mvx.Trace(MvxTraceLevel.Error, "Exception: {0}", e.Exception.ToLongString());
        var logReader = new AndroidLogReader();
        var logMessages = logReader.ReadLog("mvx:E");
        var customData = new Dictionary<string, string> { { "logMessage", logMessages } };
        var session = SessionController.Instance;
        var user = new RaygunIdentifierMessage(session.UserName + " " + session.Company);
        var rayMessage = RaygunMessageBuilder.New
            .SetEnvironmentDetails()
            .SetMachineName(Environment.MachineName)
            .SetClientDetails()
            .SetExceptionDetails(exception)
            .SetUser(user)
            .SetUserCustomData(customData)
            .Build();
        RaygunClient.Current.Send(rayMessage);
    }
}

My DebugTrace.cs looks like so:

    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} {2}", level, message);
        }
    }
}

And my AndroidLogReader looks like so:

    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();
        }
    }
}

With these things in place I now get custom data attached to all of my Raygun errors that includes the stack trace for all errors from Mvx. Thank you so much @Kiliman for pointing me towards the building blocks to get this to work!

PkL728
  • 955
  • 8
  • 21