Search code examples
c#error-handlingwindows-runtimewindows-store-appsmvvmcross

Is there any Error Logging Framework in Mvvmcross?


I am building a cross platform application using Mvvmcross. And I wanted to know if there is any in built error logging framework already existing in Mvvmcross library.

I am looking for something which will log all the errors into a file in the application data. I can simply built my own in the Windows Store app but how do i log the errors which arise in Core Library as I cant access Application Data folder there ?

I saw one Mvx.Error() method. What is the purpose of it ?


Solution

  • I saw one Mvx.Error() method. What is the purpose of it ?

    Mvx.Trace(), Mvx.Warning() and Mvx.Error() all provide simple APIs to log information.

    The log output itself goes to a singleton registered during Setup. You can easily implement your own implementation of this:

    public class DebugTrace : 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);
            }
        }
    }
    

    and this can easily be registered during Setup:

    public class Setup : MvxAndroidSetup
    {
        public Setup(Context applicationContext) : base(applicationContext)
        {
        }
    
        protected override IMvxApplication CreateApp()
        {
            return new Core.App();
        }
    
        protected override IMvxTrace CreateDebugTrace()
        {
            return new DebugTrace();
        }
    }
    

    In apps, I generally implement my own app specific logging in the Core class using one or more unique tags - this allows me to then filter on the tag - e.g. I might have something like:

    public static class MyApp
    {
        public static void NetTrace(string message, params object[] args)
        {
            Mvx.TaggedTrace("MyAppNet", message, args);
        }
    
        public static void NetError(string message, params object[] args)
        {
            Mvx.TaggedError("MyAppNet", message, args);
        }
    
        public static void VmTrace(string message, params object[] args)
        {
            Mvx.TaggedTrace("MyAppVm", message, args);
        }
    
        public static void VmError(string message, params object[] args)
        {
            Mvx.TaggedError("MyAppVm", message, args);
        }
    }
    

    For logging outside of the debugger, there are several libraries around - e.g. things like NLog - which you can easily hook up to MvxTrace