Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 62 additions & 9 deletions samples/Sentry.Samples.AspNetCore.Mvc/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
Sample usages of the Sentry SDK for ASP.NET Core on an MVC app
# Sentry for ASP.NET Core MVC

Start by changing the DSN in `appsettings.json`, `Sentry:Dsn` property with your own.
No DSN yet? Get one for free at https://sentry.io/ to give this sample a run.
An ASP.NET Core MVC app (based on `dotnet new mvc`) that shows how to add Sentry with the
[Sentry.AspNetCore](https://www.nuget.org/packages/Sentry.AspNetCore) package, and what Sentry captures when
things go wrong.

Blocking detection:
* It's turned on via option `CaptureBlockingCalls`
* In the `HomeController` there's an action that causes a blocking call on an async method.
* You can trigger it with:
* `GET http://localhost:5001/home/block/true`
Sentry is added with a single call to `builder.UseSentry(...)` in `Program.cs`. The options are bound from the
`Sentry` section of `appsettings.json` first, then the callback in `Program.cs` sets the rest in code. Between them
they show:

Results `Was blocking? True` and an event captured in Sentry.
- **Errors**: unhandled exceptions in controllers and views, and exceptions captured explicitly with `IHub`.
- **Logging**: `ILogger` messages become breadcrumbs (`MinimumBreadcrumbLevel`), events (`MinimumEventLevel`)
and [structured logs](https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/logs/) (`EnableLogs`).
- **Tracing**: every request is traced (`TracesSampleRate`), and a `TracesSampler` sets a different rate for one
route.
- **Blocking call detection**: `CaptureBlockingCalls` reports sync-over-async calls, such as `Task.Wait()`, made
on an async code path.
- **Event processors**: `ExampleEventProcessor` adds data to every event, and `SpecialExceptionProcessor` runs only
for `SpecialException`. Both are registered with dependency injection.
- **User feedback**: a form that sends feedback, with an optional screenshot, using `SentrySdk.CaptureFeedback`.
- **Tunneling**: `AddSentryTunneling` and `UseSentryTunneling` let a browser SDK send its events through this app,
past ad blockers.

**See the [ASP.NET Core documentation](https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/) for details on
every option.**

## Running this sample

You need a DSN, which tells the SDK where to send events. No Sentry account yet?
[Sign up for free](https://sentry.io/signup/), create a .NET project and copy its DSN. Then do one of the
following:

- Set the `SENTRY_DSN` environment variable before building and running the sample:

```sh
export SENTRY_DSN=https://<key>@<org>.ingest.sentry.io/<project>
```

- Or enter your DSN in [`samples/SamplesShared.cs`](../SamplesShared.cs) and remove the `#error` line there.

Then, from this folder:

```sh
dotnet run
```

Open http://localhost:5001 and try the pages below. Each one sends something you can find in Sentry. The first
request after startup also sends a `Failed to determine the https port for redirect.` warning, because the sample
only listens on HTTP.

| Page or route | What happens | What you'll see in Sentry |
| --- | --- | --- |
| **Home** form or AJAX button (`POST /Home/PostIndex`) | Neither sends a `params` value, so a `Param is null!` warning is logged. Then two tasks throw in parallel, and the app wraps the resulting `AggregateException` in an `InvalidOperationException` that it doesn't handle. | An error with both inner exceptions, the request body, and extra data attached through `Exception.Data`, plus a warning event for `Param is null!`. |
| **Razor Exception** (`/Home/Razor`) | A warning is logged, then the view throws a `SpecialException` while it renders. | An error tagged `IsSpecial` by `SpecialExceptionProcessor`, and a warning event that has a stack trace because of `AttachStackTrace`. |
| **Explicit Capture** (`/Home/Capture`) | The action catches an exception and sends it with `IHub.CaptureException`. | A handled error with the tag and extra data set on the scope just before it. |
| **Action Throws** (`/Home/Action`) | The controller action throws. | An unhandled error with the request details. |
| **Submit Feedback** (`/home/feedback`) | You fill in a form, optionally with a screenshot. | An entry under **User Feedback**, with the screenshot attached. |
| `/home/block/true` | A blocking `Task.Wait()` call on an async method. Use `/home/block/false` to compare. | An event reporting the blocking call. |
| `/home/suppress/true` | The same blocking call, inside `SuppressBlockingDetection`. | Nothing, since detection is suppressed. `/home/suppress/false` reports it. |
| `/home/sampler` | A route the `TracesSampler` in `Program.cs` samples at 30%. | A transaction for roughly one request in three. |

Every other page is sent as a transaction too, so you can follow each one in Sentry's tracing views.

`Debug` is turned on in `appsettings.json` with `DiagnosticLevel` set to `Error`, so the SDK prints its own errors to
the console. Set `DiagnosticLevel` to `Info` to see each event the SDK captures and sends.
3 changes: 1 addition & 2 deletions samples/Sentry.Samples.AspNetCore.Mvc/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Example configuration via JSON. Only a subset of the settings below:
// All Sentry settings can also be configured via code or environment variables:
"Sentry": {
// The DSN can also be set via environment variable
//"Dsn": "TODO: Configure your DSN here and uncomment this line",
// This sample takes the DSN from the SENTRY_DSN environment variable or from SamplesShared.cs. See README.md.
// Opt-in for payload submission
"MaxRequestBodySize": "Always",
// Sends Cookies, User Id when one is logged on and user IP address to sentry. It's turned off by default.
Expand Down
46 changes: 46 additions & 0 deletions samples/Sentry.Samples.AspNetCore.Serilog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Sentry for ASP.NET Core with Serilog

A minimal ASP.NET Core app that logs with [Serilog](https://serilog.net/) and sends those logs, and any unhandled
exceptions, to Sentry. It uses the [Sentry.AspNetCore](https://www.nuget.org/packages/Sentry.AspNetCore) and
[Sentry.Serilog](https://www.nuget.org/packages/Sentry.Serilog) packages.

Everything is in `Program.cs`:

- `SentrySdk.Init` runs before the web host is built, so a crash during startup is still captured.
`builder.UseSentry(...)` takes over once the app is running.
- `WriteTo.Sentry(...)` adds the Sentry sink to Serilog. Debug messages and above become breadcrumbs
(`MinimumBreadcrumbLevel`), errors and above are sent as events (`MinimumEventLevel`), and every message is also
sent as a [structured log](https://docs.sentry.io/platforms/dotnet/guides/serilog/logs/).
- `o.UseSerilog()` copies properties from Serilog's `LogContext` onto Sentry events.
- Logs written through the ASP.NET Core `ILogger` go to Serilog too, so they reach Sentry the same way.
- The remaining options, such as `SendDefaultPii` and `AttachStackTrace`, are read from `appsettings.json`.

**See the [Serilog documentation](https://docs.sentry.io/platforms/dotnet/guides/serilog/) for details.**

## Running this sample

You need a DSN, which tells the SDK where to send events. No Sentry account yet?
[Sign up for free](https://sentry.io/signup/), create a .NET project and copy its DSN. Then do one of the
following:

- Set the `SENTRY_DSN` environment variable before building and running the sample:

```sh
export SENTRY_DSN=https://<key>@<org>.ingest.sentry.io/<project>
```

- Or enter your DSN in [`samples/SamplesShared.cs`](../SamplesShared.cs) and remove the `#error` line there.

Then, from this folder:

```sh
dotnet run
```

The app listens on http://localhost:5002. Every request logs a debug message, an information message and an error,
so **each request you make sends an error event to Sentry**, carrying the first two as breadcrumbs. On top of that:

| Route | What happens | What you'll see in Sentry |
| --- | --- | --- |
| `/greetings` | Responds with `Hello, World!`. | Only the error logged for every request. |
| `/throw` | Sets a tag and extra data on the scope, logs an information message and a warning, then throws. | An unhandled exception with the `Sample` tag, the extra data, and every log message from the request as breadcrumbs. |
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Example configuration via JSON. Only a subset of the settings below:
// All Sentry settings can also be configured via code or environment variables:
"Sentry": {
// This sample sets the DSN in Program.cs. You can set it here instead, or via the SENTRY_DSN environment variable.
// Enable Sentry tracing features
"TracesSampleRate": 1.0,
// This sample takes the DSN from the SENTRY_DSN environment variable or from SamplesShared.cs. See README.md.
// Opt-in for payload submission
"MaxRequestBodySize": "Always",
// Sends Cookies, User Id when one is logged on and user IP address to sentry. It's turned off by default.
Expand Down
Loading