Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

mono - MvvmCross Mvx.Trace usage

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...