Mvvm跨Mvx.Trace的用法 [英] MvvmCross Mvx.Trace usage
问题描述
我正在使用MvvmCross. 在库中,我看到了Mvx.Trace方法的用法,但没有输出到控制台/输出窗口. 如何使用?
I'm using MvvmCross. In the library I see usage of Mvx.Trace method, but no output to the console/output window. How to use it?
p.s.我已经设置了编译器常量Trace = true
p.s. I have set the compiler constant Trace = true
谢谢.
推荐答案
MvvmCross DebugTrace均通过IMvxTrace
MvvmCross DebugTrace is all routed via a singleton implementation of IMvxTrace
每个平台都提供了不同的实现方式:
Each platform provides different implementations of this:
- Windows平台使用
Debug
跟踪(因为Trace
本身并非在所有Windows平台上都可用) - Android使用
Debug
和Androidlog
的双重日志
- iOS使用
Debug
和iOSConsole
的双重日志
- the Windows platforms use
Debug
trace (becauseTrace
itself is not available on all the Windows platforms) - Android uses a dual log of both
Debug
and the Androidlog
- iOS uses a dual log of both
Debug
and the iOSConsole
这在MvvmCross的早期版本中效果很好,尤其是在人们直接链接到MvvmCross源代码的地方,因此可以直接绑定到调试或发布MvvmCross二进制文件.
This worked well in earlier versions of MvvmCross, especially where people linked directly to the MvvmCross source code and so could bind directly to either the debug or release MvvmCross binaries.
但是...由于人们现在越来越多地使用Nuget发行的Binaries-当然已经编译了Debug
,因此人们常常会问如何使用它"?
However... because people are now increasingly using the release Binaries from Nuget - in which Debug
is of course compiled out, then this often leaves people asking "How to use it?"
最简单的方法是让您在Setup
类中覆盖IMvxTrace的初始化.
The easiest way is for you to override the initialisation of IMvxTrace in your Setup
class.
这很容易做到-例如:
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#也应该很容易做到.
If you wanted to include different implementations for debug/release; if you wanted to switch this on/off at runtime; if you wanted to filter on MvxTraceLevel
or tag
; or if you wanted to use some third party logging engine then this should also be easy to do using simple C# on each platform.
这篇关于Mvvm跨Mvx.Trace的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!