我正在使用MvvmCross。 在库中,我看到了Mvx.Trace方法的用法,但没有输出到控制台/输出窗口。 怎么用?
P.S。我已经设置了编译器常量Trace = true
感谢。
答案 0 :(得分:21)
MvvmCross DebugTrace全部通过IMvxTrace
每个平台提供不同的实现:
Debug
跟踪(因为Trace
本身并非在所有Windows平台上都可用)Debug
和Android log
Debug
和iOS Console
这在早期版本的MvvmCross中运行良好,尤其是人们直接链接到MvvmCross源代码,因此可以直接绑定到调试或释放MvvmCross二进制文件。
然而......因为人们现在越来越多地使用Nuget发布的二进制文件 - 其中Debug
当然是编译出来的,那么这通常会让人们问“如何使用它?”
最简单的方法是覆盖Setup
课程中IMvxTrace的初始化。
这很容易做到 - 例如:
protected override IMvxTrace CreateDebugTrace() { return new MyDebugTrace(); }
其中MyDebugTrace
类似于:
public class MyDebugTrace : 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);
}
}
}
如果你想包含不同的调试/发布实现;如果你想在运行时打开/关闭它;如果您想过滤MvxTraceLevel
或tag
;或者如果你想使用某些第三方日志引擎,那么在每个平台上使用简单的C#也应该很容易。
答案 1 :(得分:4)
使用Stuart提供的当前解决方案在iOS上遇到问题。
使用Debug.WriteLine
在MvxTrace中在VS中运行调试会话可能会非常慢,具体取决于您的应用程序日志记录的详细程度。 Trace.WriteLinte
也是如此。
使用Console.WriteLine
代替(如MvvmCross MvxDebugTrace
那样),但不受影响。
有关“更多”详细信息,请参阅此Xamarin错误项:https://bugzilla.xamarin.com/show_bug.cgi?id=13538#c4