public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            if (formatter != null)
            {
                string message = formatter(state, exception);

                StringBuilder sbMessage = new StringBuilder(message);

                if (!string.IsNullOrWhiteSpace(exception?.Message))
                {
                    sbMessage
                    .AppendLine()
                    .AppendLine($"{exception.GetType().FullName}: {exception.Message}");
                }

                var innerException = exception?.InnerException;

                while (innerException != null)
                {
                    if (!string.IsNullOrWhiteSpace(innerException.Message))
                    {
                        sbMessage
                        .AppendLine()
                        .AppendLine($"{innerException.GetType().FullName}: {innerException.Message}");
                    }

                    innerException = innerException.InnerException;
                }

                RemoteClientLogModel model = new RemoteClientLogModel
                {
                    Id         = Guid.NewGuid(),
                    Level      = (RemoteClientLogLevel)logLevel, //1:1 mapping between log level values so this is ok
                    Message    = sbMessage.ToString(),
                    StackTrace = exception?.StackTrace,
                    Source     = "Garden Visualiser - WebGL"
                };

                var task = LogService.PostAsync(model);
                task.GetAwaiter().OnCompleted(() =>
                {
                    //In the event that the log message couldn't be uploaded to the server, write this out to the console instead
                    if (task.IsFaulted)
                    {
                        LogLoggerException(task.Exception);
                    }
                });
            }
        }
Exemplo n.º 2
0
        public virtual async Task <RemoteClientLogServiceResult> PostAsync(RemoteClientLogModel model, CancellationToken cancellationToken = default)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                Guard.ArgumentNotNull(model, nameof(model));

                var response = await m_NetworkManager.PerformRequest(m_ApiUrl, HttpMethodType.Post, model, requiresAuthentication : false, showLoadingScreen : false);

                switch (response?.Status)
                {
                case HttpStatusCode.OK:
                    return(new RemoteClientLogServiceResult(true, null));

                default:
                    return(new RemoteClientLogServiceResult(false, response?.Text));
                }
            }
            catch (Exception exc)
            {
                Debug.LogError(exc.Message);
                throw;
            }
        }