MvvmCross Mvx.Trace 用法 [英] MvvmCross Mvx.Trace usage

查看:12
本文介绍了MvvmCross Mvx.Trace 用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MvvmCross.在库中,我看到 Mvx.Trace 方法的使用,但没有输出到控制台/输出窗口.如何使用?

附言我已经设置了编译器常量 Trace = true

谢谢.

解决方案

MvvmCross DebugTrace 全部通过 IMvxTrace

的单例实现路由

每个平台都提供了不同的实现:

  • Windows 平台使用 Debug 跟踪(因为 Trace 本身并非在所有 Windows 平台上都可用)
  • Android 使用 Debug 和 Android log
  • 的双重日志
  • iOS 使用 Debug 和 iOS Console
  • 的双重日志

这在 MvvmCross 的早期版本中运行良好,尤其是人们直接链接到 MvvmCross 源代码,因此可以直接绑定到调试或发布 MvvmCross 二进制文件.

然而……因为人们现在越来越多地使用来自 Nuget 的发行版二进制文件 - 其中 Debug 当然是编译出来的,所以这经常让人们问如何使用它?">

最简单的方法是在您的 Setup 类中覆盖 IMvxTrace 的初始化.

这很容易做到 - 例如:

 protected override IMvxTrace CreateDebugTrace() { return new MyDebugTrace();}

其中 MyDebugTrace 类似于:

公共类 MyDebugTrace : IMvxTrace{public void Trace(MvxTraceLevel level, string tag, Func 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){尝试{Debug.WriteLine(string.Format(tag + ":" + level + ":" + message, args));}捕获(格式异常){Trace(MvxTraceLevel.Error,tag,{0} {1} {2} 的跟踪过程中出现异常",级别,消息);}}}

如果您想为调试/发布包含不同的实现;如果你想在运行时打开/关闭它;如果你想过滤 MvxTraceLeveltag;或者,如果您想使用某些第三方日志引擎,那么在每个平台上使用简单的 C# 也应该很容易做到.

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. I have set the compiler constant Trace = true

Thanks.

解决方案

MvvmCross DebugTrace is all routed via a singleton implementation of IMvxTrace

Each platform provides different implementations of this:

  • the Windows platforms use Debug trace (because Trace itself is not available on all the Windows platforms)
  • Android uses a dual log of both Debug and the Android log
  • iOS uses a dual log of both Debug and the iOS Console

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.

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?"

The easiest way is for you to override the initialisation of IMvxTrace in your Setup class.

This is easy to do - e.g.:

   protected override IMvxTrace CreateDebugTrace() { return new MyDebugTrace(); }

where MyDebugTrace is something like:

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

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.

这篇关于MvvmCross Mvx.Trace 用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆