Using MvvmCross Mvx.Trace

I am using MvvmCross. In the library, I see the use of the Mvx.Trace method, but they do not display it in the console / output window. How to use it?

ps I set the compiler constant Trace = true

Thanks.

+7
source share
2 answers

MvvmCross DebugTrace everything is routed using a single-line implementation of IMvxTrace

Each platform provides various implementations of this:

  • on Windows platforms, Debug tracing is used (since Trace itself is not available on all Windows platforms)
  • Android uses a double log of both Debug and Android log
  • iOS uses a double log of both Debug and iOS Console

This worked well in earlier versions of MvvmCross, especially where people directly linked to the MvvmCross source code and therefore could directly link to the MvvmCross debug or release binaries.

However ... because people are increasingly using the Nuget Binaries release, in which Debug is of course compiled, then this often leaves people asking "How to use it?"

The easiest way is to override IMvxTrace initialization in your Setup class.

This is easy to do - for example:

  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 want to enable various implementations for debug / release; if you want to enable or disable it at run time; if you want to filter on MvxTraceLevel or tag ; or if you want to use some kind of third-party logging mechanism, this should also be easy to do using simple C # on each platform.

+21
source

Just click on the issue in iOS with the current solution provided by Stuart.

Starting a debugging session in VS using MvxTrace using Debug.WriteLine can be extremely slow depending on how much application logging is done. The same goes for Trace.WriteLinte . Instead, use Console.WriteLine (as MvvmCross MvxDebugTrace ), which is not affected.

See this Xamarin error item for more details: https://bugzilla.xamarin.com/show_bug.cgi?id=13538#c4

+4
source

All Articles