5

我正在使用 MvvmCross 并尝试使用一些方法在我的报告工具中获取 MvxTrace。在这种情况下,我使用的是 Raygun。Raygun 让我可以选择在我想抛出的错误消息中包含其他消息,这就是我认为我必须使用它来实现这一点。基本上我想在代码中做这样的事情:

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

我怎么能把它挂起来?当我查看 Trace 设置时,我感到很困惑。我假设我需要对我用来注入的 DebugTrace 文件做一些事情。现在它看起来像这样:

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

我可以做一些与 IMvxTrace 逻辑挂钩的事情,以将内部异常等附加到我的 RaygunClient 吗?我很难看出是什么导致了特定的错误,因为如果我保持原样,我会得到如下所示的错误:

[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)

正如该消息所暗示的那样,如果我可以使用 MvxTrace 来准确追踪初始化此 ViewModel 失败的原因,那将非常有帮助。有什么建议么?

4

2 回答 2

2

这就是我在安卓上的做法。我使用Android.Util.Log类。然后,这会将消息记录到 Android 设备日志中。

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

然后,您可以使用以下命令获取日志:

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();
        }
    }
}
于 2014-08-05T12:54:56.693 回答
1

以下是我为使它为我工作所做的工作:

我有一个用于所有 Android 活动的 BaseView。我利用这个 BaseView 来连接和记录未处理的异常,如下所示:

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

我的 DebugTrace.cs 看起来像这样:

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

我的 AndroidLogReader 看起来像这样:

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

有了这些东西,我现在可以将自定义数据附加到我的所有 Raygun 错误,其中包括来自 Mvx 的所有错误的堆栈跟踪。非常感谢@Kiliman 将我指向构建模块以使其正常工作!

于 2014-08-05T20:40:05.677 回答