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

问题描述 投票:0回答:1

我不确定我是否走在正确的轨道上。我在这个方法中放了很多跟踪事件来跟踪用户流。任何人都知道我是否投入太多。

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中但是有限制的字符可以作为属性传递。

xamarin azure-application-insights diagnostics
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.