Xamarincommunitytoolkit: [Discussion] Logging strategy

Created on 10 Jun 2020  路  4Comments  路  Source: xamarin/XamarinCommunityToolkit

Summary

What should we do when the state of one component is incorrect due to violated prerequisites, incorrect behavior (aka bug)?
To defeat the "fail silently" vs "crash in prod" dilemma, we could have a configurable internal logger that will log any warning or error detected in the components.

API Changes

Something close to this maybe:

https://github.com/roubachof/Sharpnado.Shadows/blob/master/Shadows/Shadows/InternalLogger.cs

The logger can be enabled or disabled by the user.
The user could also provide its own delegate to handle the error messages.
It could be useful when attaching logs to crash reports (nice feature from app center).

Intended Use Case

  1. Have error feedback while debugging
  2. Have error trace in crash reports in production
feature-request needs-more-discussion

Most helpful comment

Is there a reason for not using ILogger? This gives a lot of flexibility to the end user.

All 4 comments

Is there a reason for not using ILogger? This gives a lot of flexibility to the end user.

Absolutely none. Seems like a good idea to implement it :)

Agree with Bart here, if there are existing constructs within .NET we can leverage for this we definitely should.

my 10 cents here:

I think that we can use an instance for this, it will provide a way to extend this class and maybe integrate it with appcenter...

I mean, maybe some logs are interesting to track in the appcenter or another "issues tracker", like a local database.

```C#

protected override OnLog(string message, StatusLevel level)
{
if (level == StatusLevel.Critical)
TrackOnMyCustomIssueTracker(message);

base.OnLog(string message, StatusLevel level);
}

Another thing is catch unhandled exceptions, we can catch it with app domain:

```C#
        AppDomain.CurrentDomain.UnhandledException += OnUnhandledExceptionDetected;

        private static void OnUnhandledExceptionDetected(object sender, UnhandledExceptionEventArgs args) {
            var e = (Exception)args?.ExceptionObject;
            //do logging strategy
        }

And, for Android, we have the Java.Lang.Thread.DefaultUncaughtExceptionHandler

```C#
public class AndroidUncaughtExceptionTracker : IUncaughtExceptionHandler
{
private readonly IUncaughtExceptionHandler _innerHandler;

    protected AndroidUncaughtExceptionTracker(IUncaughtExceptionHandler inner)
    {
        _innerHandler = inner;
    }

    public void OnUnhandledExceptionDetected(object sender, UnhandledExceptionEventArgs args) {
        var e = (System.Exception)args?.ExceptionObject;

        //do logging strategy
        _innerHandler?.OnUnhandledExceptionDetected(sender, args);
    }

    public static void Register()
    {
        if (Java.Lang.Thread.DefaultUncaughtExceptionHandler is AndroidUncaughtExceptionTracker)
            return;

        var exceptionTracker = new AndroidUncaughtExceptionTracker(Java.Lang.Thread.DefaultUncaughtExceptionHandler);
        Java.Lang.Thread.DefaultUncaughtExceptionHandler = exceptionTracker;
    }

}
```

Was this page helpful?
0 / 5 - 0 ratings