Developer

Additional Logging and Tracing Code Samples

These examples illustrate how to include code in your apps for uploading logs and traces for Store apps and .NET applications.

These examples illustrate basic implementations, and as a result the upload process cannot be canceled. To cancel the upload, create a CancellationTokenSource and pass its CancellationToken as the last parameter of the SendAsync method of the netclient's instance. Then you can show a button, for example "Cancel" and then you can signal the CancellationToken. The cancellation of the upload process is then handled by the Networking and Supportability libraries.

Windows Store app uploader example code

class UploadResult : SAP.Supportability.IUploadResult {
      public int ResponseStatusCode { get; set; }
      public string Hint { get; set; }
}

class SupportabilityUploader : SAP.Supportability.IUploader {
      SAP.Net.Http.HttpClient clientRef = null; 
      Uri serverUri= null;

      public SupportabilityUploader(SAP.Net.Http.HttpClient client, Uri host, bool uploadBtx = true) {
            if (client == null) throw new ArgumentNullException("client");
            if (host == null) throw new ArgumentNullException("host");

            this.serverUri = new UriBuilder(host.Scheme, host.Host, host.Port, (uploadBtx ? "btx" : "clientlogs")).Uri;
            this.clientRef = client;
      }

public IAsyncOperation<SAP.Supportability.IUploadResult> SendAsync(IReadOnlyDictionary<string, string> headers, Windows.Storage.Streams.IInputStream payload) {
            return Task.Run<SAP.Supportability.IUploadResult>(async () => {
                  var result = await this.clientRef.SendAsync(() => {
                        var request = new HttpRequestMessage(HttpMethod.Post, this.serverUri) {
                              Content = new StreamContent(payload.AsStreamForRead())
                        };

                        foreach (var header in headers) {
                              request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value);
                        }

                        return request;
                  });

                  return new UploadResult() { ResponseStatusCode = (int)result.StatusCode,
                                                            Hint = await result.Content.ReadAsStringAsync() };
            }).AsAsyncOperation();
      }
}

Windows .NET application uploader example code (only the method which is different from the Store app)

public async Task<SAP.Supportability.IUploadResult> SendAsync(IReadOnlyDictionary<string, string> headers, System.IO.Stream payload) {
      var result = await this.clientRef.SendAsync(() => {
            var request = new HttpRequestMessage(HttpMethod.Post, this.serverUri) {
                  Content = new StreamContent(payload)
            };

            foreach (var header in headers) {
                  request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }

            return request;
      });

      return new UploadResult() { ResponseStatusCode = (int)result.StatusCode,
                                                Hint = await result.Content.ReadAsStringAsync() };
}

Log creation upload example code for Store Apps and .Net applications

var logManager = SAP.Supportability.SupportabilityFacade.Instance.ClientLogManager;
logManager.SetLogLevel(SAP.Supportability.Logging.ClientLogLevel.Info);
logManager.SetLogDestination(SAP.Supportability.Logging.ClientLogDestinations.FileSystem | SAP.Supportability.Logging.ClientLogDestinations.Console);
var logger = logManager.GetLogger("testLogger");

logger.LogWarning("sample");
logger.LogError("sample error message");
logger.LogError("sample log 2");

string message = null;
try {
      await SAP.Supportability.SupportabilityFacade.Instance.ClientLogManager.UploadClientLogsAsync(new SupportabilityUploader(httpClient, serverUri, false));
} catch (Exception ex) {
      var supportabilityException = ex as SAP.Supportability.ISupportabilityException;
      message = ex.Message + ((supportabilityException != null) ? ("(" + supportabilityException.UploadResult.ResponseStatusCode + ")") : "");
}

BTX generation and upload example for Store apps and .Net applications

var traceManager = (SAP.Supportability.Tracing.E2ETraceManager)SAP.Supportability.SupportabilityFacade.Instance.E2ETraceManager;
traceManager.ClientHost = "WinDemo-Client";
traceManager.TraceLevel = SAP.Supportability.Tracing.E2ETraceLevel.Low;

var transaction = await traceManager.StartTransactionAsync("NewTransactionWin");

var step = transaction.StartStep();
var request = step.StartRequest();
request.SetRequestLine("GET http://www.test.com HTTP/1.1");

request.SetRequestHeaders(new Dictionary<string, string> { 
      {"SAP-PASSPORT",request.PassportHttpHeader} ,
      {"X-CorrelationID","correlationID0101"} 
});
request.SetByteCountSent(100);
request.EndRequest();
step.EndStep();

transaction.EndTransaction();

string message = null;
try {
      await SAP.Supportability.SupportabilityFacade.Instance.E2ETraceManager.UploadBtxAsync(new SupportabilityUploader(httpClient, serverUri));
} catch (Exception ex) {
      var supportabilityException = ex as SAP.Supportability.ISupportabilityException;
      message = ex.Message + ((supportabilityException != null) ? ("(" + supportabilityException.UploadResult.ResponseStatusCode + ")") : "");
}