Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
restored sample
  • Loading branch information
bitsandfoxes committed Sep 23, 2024
commit c3ebbab2e2bf73d429010140f702d7173a64268b
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();
}
}