Skip to content

Commit

Permalink
restored sample
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes committed Sep 23, 2024
1 parent 9f77eb7 commit c3ebbab
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions samples/Sentry.Samples.Console.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,64 @@
options.TracesSampleRate = 1.0;
});

// This starts a new transaction and attaches it to the scope.
var transaction = SentrySdk.StartTransaction("Program Main", "function");
SentrySdk.ConfigureScope(scope => scope.Transaction = transaction);

// Do some work. (This is where you'd have your own application logic.)
await FirstFunction();
await SecondFunction();
await ThirdFunction();

// Always try to finish the transaction successfully.
// Unhandled exceptions will fail the transaction automatically.
// Optionally, you can try/catch the exception, and call transaction.Finish(exception) on failure.
transaction.Finish();

async Task FirstFunction()
{
// This is an example of making an HttpRequest. A trace us automatically captured by Sentry for this.
var messageHandler = new SentryHttpMessageHandler();
var httpClient = new HttpClient(messageHandler, true);
var html = await httpClient.GetStringAsync("https://example.com/");
Console.WriteLine(html);
}

async Task SecondFunction()
{
var span = transaction.StartChild("function", nameof(SecondFunction));

try
{
// Simulate doing some work
await Task.Delay(100);

// Throw an exception
throw new ApplicationException("Something happened!");
}
catch (Exception exception)
{
// This is an example of capturing a handled exception.
SentrySdk.CaptureException(exception);
span.Finish(exception);
}

span.Finish();
}

async Task ThirdFunction()
{
var span = transaction.StartChild("function", nameof(ThirdFunction));
try
{
// Simulate doing some work
await Task.Delay(100);

// This is an example of an unhandled exception. It will be captured automatically.
throw new InvalidOperationException("Something happened that crashed the app!");
}
finally
{
span.Finish();
}
}

0 comments on commit c3ebbab

Please sign in to comment.