Xamarin形式:我是否在代码中加入了很多TrackEvent,并且TrackError可以在非catch语句中使用

时间:2019-02-28 22:43:15

标签: xamarin azure-application-insights diagnostics

我不确定我走的路是否正确。我在此方法中放置了很多跟踪事件来跟踪用户流。有人给我一个想法,我是否投入过多。

try
            {
                #region AppCenter Analytics
                Diagnostic.TrackEvent("Start Refreshing Token",
                    new Dictionary<string, string>() {
                        { "Username",  username}
                    });
                #endregion

                #region AppCenter Diagnostics --Empty
                #endregion

                if (String.IsNullOrEmpty(refreshToken))
                {
                    #region AppCenter Analytics
                    Diagnostic.TrackEvent("Empty Refreshed Token",
                        new Dictionary<string, string>() {
                        { "Username",  username}
                        });
                    #endregion

                    #region AppCenter Diagnostics --Empty
                    #endregion

                    return new CommonResult<TokenModel>()
                    {
                        Data = null,
                        Code = (int) ErrorCode.ErrorRefreshTokenEmpty,
                        IsSuccess = false,
                        ErrorMessage = string.Empty,
                        Error = new ErrorData(new Exception("Refresh token is empty."), new string[] { })
                    };
                }

                #region AppCenter Analytics
                Diagnostic.TrackEvent("Get Discovery Endpoint",
                    new Dictionary<string, string>() {
                        { "Username",  username}
                    });
                #endregion

                #region Diagnostics -- Empty
                #endregion

                var disco = await GetDiscoveryResponseAsync();

                if (disco.IsError)
                {
                    #region AppCenter Analytics
                    Diagnostic.TrackEvent("Error Discovery Endpoint",
                        new Dictionary<string, string>() {
                        { "Username",  username}
                        });
                    #endregion

                    #region Diagnostics -- Empty
                    #endregion

                    return new CommonResult<TokenModel>() 
                    { 
                        Data = null, 
                        IsSuccess = false,
                        Code = (int) ErrorCode.ErrorDiscoveryEndpoint,
                        ErrorMessage = string.Empty
                    };
                }

                #region AppCenter Analytics
                Diagnostic.TrackEvent("Get Refresh Token Endpoint",
                    new Dictionary<string, string>() {
                        { "Username",  username},
                        { "Endpoint", disco.TokenEndpoint}
                    });
                #endregion

                #region Diagnostics -- Empty
                #endregion

                var httpClient = new HttpClient();
                var response = await httpClient.RequestRefreshTokenAsync(new RefreshTokenRequest()
                {
                    Address = disco.TokenEndpoint,

                    ClientId = clientId,
                    ClientSecret = clientSecret,
                    Scope = scope,
                    RefreshToken = refreshToken
                });

                if (response.IsError)
                {
                    #region AppCenter Analytics
                    Diagnostic.TrackEvent("Error getting refresh token",
                        new Dictionary<string, string>() {
                            { "Username",  username },
                            { "Endpoint", disco.TokenEndpoint } 
                        });
                    #endregion

                    #region Diagnostics -- Empty
                    #endregion

                    return new CommonResult<TokenModel>()
                    {
                        Data = null,
                        IsSuccess = false,
                        Code = (int)ErrorCode.ErrorRefreshToken,
                        ErrorMessage = string.Empty
                    };
                }

                var data = new TokenModel()
                {
                    ExpiresIn = response.ExpiresIn.ToString(),
                    TokenType = response.TokenType,
                    AccessToken = response.AccessToken,
                    RefreshToken = response.RefreshToken
                };

                return new CommonResult<TokenModel>() { Data = data, IsSuccess = true, ErrorMessage = String.Empty };
            }
            catch (Exception exception)
            {
                #region AppCenter Analytics
                Diagnostic.TrackEvent("Error Getting Refresh Token",
                    new Dictionary<string, string>() {
                        { "Username",  username}
                    });
                #endregion

                #region Diagnostics -- Empty
                Diagnostic.TrackErrorRequired(exception,
                    new Dictionary<string, string> {
                        { "Username", username },
                    });
                #endregion

                return new CommonResult<TokenModel>()
                {
                    Data = null,
                    IsSuccess = false,
                    Code = (int) ErrorCode.ErrorRefreshTokenUnknown,
                    ErrorMessage = string.Empty
                };
            }
        }

在if语句中,在执行以前的代码时有错误,我还应该调用Crashes.TrackError来跟踪错误,或者不使用Crashes.TrackError,因为它不会崩溃。

如果不应使用Crashes.TrackError,则记录这些错误的最佳实践是什么。我可以放在TrackEvent中,但可以作为属性传递的字符数有限。

1 个答案:

答案 0 :(得分:0)

TrackError专用于跟踪捕获的异常。