我正在使用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失败,那将非常有用。有什么建议吗?
答案 0 :(得分:2)
这就是我在Android上的表现。我使用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();
}
}
}
答案 1 :(得分:1)
以下是我为此工作所做的工作:
我有一个我用于所有Android活动的BaseView。我使用这个BaseView来连接并记录Unhandled Exceptions,如下所示:
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将我指向构建基块以使其工作!