# Options | Sentry for .NET

For example, initialize with `SentrySdk.Init` in your `Program.cs` file:

```csharp
SentrySdk.Init(options =>
{
    // A Sentry Data Source Name (DSN) is required.
    // See https://docs.sentry.io/concepts/key-terms/dsn-explainer/
    // You can set it in the SENTRY_DSN environment variable, or you can set it in code here.
    options.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";

    // When debug is enabled, the Sentry client will emit detailed debugging information to the console.
    // This might be helpful, or might interfere with the normal operation of your application.
    // We enable it here for demonstration purposes when first trying Sentry.
    // You shouldn't do this in your applications unless you're troubleshooting issues with Sentry.
    options.Debug = true;

    // Adds request URL and headers, IP and name for users, etc.
    options.SendDefaultPii = true;

    // This option is recommended. It enables Sentry's "Release Health" feature.
    options.AutoSessionTracking = true;

    // Enabling this option is recommended for client applications only. It ensures all threads use the same global scope.
    options.IsGlobalModeEnabled = false;

    // Example sample rate for your transactions: captures 10% of transactions
    options.TracesSampleRate = 0.1;
});
```

After you have configured Sentry in your code, you can also configure the [MSBuild setup](https://docs.sentry.io/platforms/dotnet/configuration/msbuild.md) for your project. While this step is optional, it can greatly improve your Sentry experience.

## [Core Options](https://docs.sentry.io/.md#core-options)

Options that can be read from an environment variable (`SENTRY_DSN`, `SENTRY_ENVIRONMENT`, `SENTRY_RELEASE`) are read automatically.

### [`Dsn`](https://docs.sentry.io/.md#dsn)

The DSN tells the SDK where to send the events. If this value is not provided, the SDK will try to read it from the `SENTRY_DSN` environment variable. If that variable also does not exist, the SDK will just not send any events.

In runtimes without a process environment (such as the browser) that fallback does not apply.

Learn more about [DSN utilization](https://docs.sentry.io/product/sentry-basics/dsn-explainer.md#dsn-utilization).

### [`StackTraceMode`](https://docs.sentry.io/.md#stack-trace-mode)

* Original - Default .NET stack trace format.
* Enhanced - Include `async`, return type, arguments, and more.

Before version 3.0.0 of the Sentry SDK for .NET, there was no special treatment for the stack trace. Sentry reported what .NET made available at runtime. This behavior now called `StackTraceMode.Original`. With the introduction of 3.0, a new default mode is `Enhanced`.

Changing this value will affect issue grouping. Since the frame significantly changes shape.

### [`IsGlobalModeEnabled`](https://docs.sentry.io/.md#is-global-mode-enabled)

Specifies whether to use global scope management mode. Should be `true` for client applications and `false` for server applications.

Example scenarios where it should be explicitly set to true:

* Universal Windows platform (UWP) applications
* WinForms applications
* Windows Presentation Foundation (WPF) applications
* Single user console applications

Defaults to `false`, unless in Blazor WASM, MAUI, Unity, or Xamarin where the default is `true`.

When Global Mode is **disabled** data stored in the scope is set on the current [ExecutionContext](https://learn.microsoft.com/en-us/dotnet/api/system.threading.executioncontext). In server applications data stored in the scope is only available in the context of the particular request in which it is created. Broadly, the ExecutionContext passes to child tasks and threads, but not to parent tasks and threads.

When Global Mode is **enabled**, a single scope stack is shared by the whole application.

##### 💡 Note

Since version 5.0.0 the transaction is always set on the current ExecutionContext, regardless of the Global Mode, so that spans from the UI don't get mixed up with transactions in background services.

There may be situations in which this isn't what you want. For example, you could start a transaction and then run multiple tasks in parallel, each of which makes an HTTP Request. By default, Sentry's [Automatic Instrumentation](https://docs.sentry.io/platforms/dotnet/tracing/instrumentation/automatic-instrumentation.md) will create spans as children of the transaction it finds stored in `Scope.Transaction` (which is now ExecutionContext specific, even in Global Mode).

You can override this behavior and force auto-instrumented spans to have a specific parent by setting `Scope.Span`. [An example of doing this](https://github.com/getsentry/symbol-collector/blob/8a0fdb2594c3028f63f180b9b56b1efea1cbce34/src/SymbolCollector.Core/Client.cs#L74-L83) can be found in the Symbol Collector.

See the [Scopes and Hubs documentation](https://docs.sentry.io/platforms/dotnet/enriching-events/scopes.md) for more information.

### [`Debug`](https://docs.sentry.io/.md#debug)

Turns debug mode on or off. If debug is enabled SDK will attempt to print out useful debugging information if something goes wrong with sending the event. The default is always `false`. It's generally not recommended to turn it on in production, though turning `debug` mode on will not cause any safety concerns.

### [`DiagnosticLevel`](https://docs.sentry.io/.md#diagnostic-level)

Enabling `debug` mode makes the SDK generate as much diagnostic data as possible. However, if you'd prefer to lower the verbosity of the Sentry SDK diagnostics logs, configure this option to set the appropriate level:

* `debug`: **default** The most verbose mode
* `info`: Informational messages
* `warning`: Warning that something might not be right
* `error`: Only SDK internal errors are printed
* `fatal`: Only critical errors are printed

For app models that don't have a console to print to, you can [customize the SDK's diagnostic logger](https://docs.sentry.io/platforms/dotnet/configuration/diagnostic-logger.md) to write to a file or to Visual Studio's debug window.

### [`Dist`](https://docs.sentry.io/.md#dist)

Sets the distribution of the application. Distributions are used to disambiguate build or deployment variants of the same release of an application. For example, the dist can be the build number of an Xcode build or the version code of an Android build. The dist has a max length of 64 characters.

### [`Release`](https://docs.sentry.io/.md#release)

Sets the release. Some SDKs will try to automatically configure a release out of the box but it's a better idea to manually set it to guarantee that the release is in sync with your deploy integrations or source map uploads. Release names are strings, but some formats are detected by Sentry and might be rendered differently. Learn more about how to send release data so Sentry can tell you about regressions between releases and identify the potential source in [the releases documentation](https://docs.sentry.io/product/releases.md) or the [sandbox](https://sandbox.sentry.io/.md?scenario=releases\&source=docs).

By default the SDK will try to read this value from the `SENTRY_RELEASE` environment variable (in the browser SDK, this will be read off of the `window.SENTRY_RELEASE.id` if available).

### [`Environment`](https://docs.sentry.io/.md#environment)

Sets the environment. This string is freeform and set by default. A release can be associated with more than one environment to separate them in the UI (think `staging` vs `prod` or similar).

By default, the SDK reports `debug` when the debugger is attached. Otherwise, the default environment is `production`.

Additionally, if you are running with the ASP.NET Core integration, you will also see the environment named as `staging`, if running in staging, or `development`, if running in development mode.

### [`SampleRate`](https://docs.sentry.io/.md#sample-rate)

Configures the sample rate for error events, in the range of `0.0` to `1.0`. The default is `1.0`, which means that 100% of error events will be sent. If set to `0.1`, only 10% of error events will be sent. Events are picked randomly.

### [`MaxBreadcrumbs`](https://docs.sentry.io/.md#max-breadcrumbs)

This variable controls the total amount of breadcrumbs that should be captured. This defaults to `100`, but you can set this to any number. However, you should be aware that Sentry has a [maximum payload size](https://develop.sentry.dev/sdk/data-model/envelopes.md#size-limits) and any events exceeding that payload size will be dropped.

### [`MaxCacheItems`](https://docs.sentry.io/.md#max-cache-items)

The maximum number of [envelopes](https://develop.sentry.dev/sdk/data-model/envelopes.md) to keep in cache. The SDKs use envelopes to send data, such as events, attachments, user feedback, and sessions to sentry.io. An envelope can contain multiple items, such as an event with a session and two attachments. Depending on the usage of the SDK, the size of an envelope can differ. If the number of envelopes in the local cache exceeds `max-cache-items`, the SDK deletes the oldest envelope and migrates the sessions to the next envelope to maintain the integrity of your release health stats. The default is `30`.

### [`AttachStacktrace`](https://docs.sentry.io/.md#attach-stacktrace)

When enabled, stack traces are automatically attached to all messages logged. Stack traces are always attached to exceptions; however, when this option is set, stack traces are also sent with messages. This option, for instance, means that stack traces appear next to all log messages.

Grouping in Sentry is different for events with stack traces and without. As a result, you will get new groups as you enable or disable this flag for certain events.

### [`SendDefaultPii`](https://docs.sentry.io/.md#send-default-pii)

If this flag is enabled, certain personally identifiable information (PII) is added by active integrations. By default, no such data is sent.

If you are using Sentry in your mobile app, read our [frequently asked questions about mobile data privacy](https://docs.sentry.io/security-legal-pii/security/mobile-privacy.md) to assist with Apple App Store and Google Play app privacy details.

This option is turned off by default.

If you enable this option, be sure to manually remove what you don't want to send using our features for managing [*Sensitive Data*](https://docs.sentry.io/data-management/sensitive-data.md).

### [`ServerName`](https://docs.sentry.io/.md#server-name)

This option can be used to supply a server name. When provided, the name of the server is sent along and persisted in the event. For many integrations, the server name actually corresponds to the device hostname, even in situations where the machine is not actually a server.

For ASP.NET and ASP.NET Core applications, the value will default to the server's name. For other application types, the value will default to the computer's name only when the `SendDefaultPii` is set to `true`, because the computer's name can be considered personally identifiable information (PII) in the case of a desktop or mobile application.

### [`InAppInclude`](https://docs.sentry.io/.md#in-app-include)

A list of string prefixes of module names that belong to the app. This option takes precedence over `in-app-exclude`.

Sentry differentiates stack frames that are directly related to your application ("in application") from stack frames that come from other packages such as the standard library, frameworks, or other dependencies. The application package is automatically marked as `inApp`. The difference is visible in [sentry.io](https://sentry.io/.md), where only the "in application" frames are displayed by default.

### [`InAppExclude`](https://docs.sentry.io/.md#in-app-exclude)

A list of string prefixes of module names that do not belong to the app, but rather to third-party packages. Modules considered not part of the app will be hidden from stack traces by default.

This option can be overridden using `InAppInclude`.

### [`SendClientReports`](https://docs.sentry.io/.md#send-client-reports)

Set this boolean to `false` to disable sending of client reports. Client reports are a protocol feature that let clients send status reports about themselves to Sentry. They are currently mainly used to emit outcomes for events that were never sent.

### [`CaptureFailedRequests`](https://docs.sentry.io/.md#capture-failed-requests)

Once enabled, this feature automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry.

## [Hooks](https://docs.sentry.io/.md#hooks)

These options can be used to hook the SDK in various ways to customize the reporting of events.

### [`SetBeforeSend`](https://docs.sentry.io/.md#set-before-send)

This function is called with an SDK-specific message or error event object, and can return a modified event object, or `null` to skip reporting the event. This can be used, for instance, for manual PII stripping before sending.

By the time `SetBeforeSend` is executed, all scope data has already been applied to the event. Further modification of the scope won't have any effect.

### [`SetBeforeBreadcrumb`](https://docs.sentry.io/.md#set-before-breadcrumb)

This function is called with an SDK-specific breadcrumb object before the breadcrumb is added to the scope. When nothing is returned from the function, the breadcrumb is dropped. To pass the breadcrumb through, return the first argument, which contains the breadcrumb object. The callback typically gets a second argument (called a "hint") which contains the original object from which the breadcrumb was created to further customize what the breadcrumb should look like.

### [`SetBeforeSendTransaction`](https://docs.sentry.io/.md#set-before-send-transaction)

This function is called with an SDK-specific transaction object, and can return a modified transaction object, or `null` to skip reporting the transaction. This can be used, for instance, for manual PII-stripping before sending.

By the time `SetBeforeSendTransaction` is executed, all scope data has already been applied to the event and further modification of the scope won't have any effect.

## [Transport Options](https://docs.sentry.io/.md#transport-options)

Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments.

### [`Transport`](https://docs.sentry.io/.md#transport)

Switches out the transport used to send events. How this works depends on the SDK. It can, for instance, be used to capture events for unit-testing or to send it through some more complex setup that requires proxy authentication.

### [`HttpProxy`](https://docs.sentry.io/.md#http-proxy)

When set, a proxy can be configured that should be used for outbound requests. This is also used for HTTPS requests unless a separate `https-proxy` is configured. However, not all SDKs support a separate HTTPS proxy. SDKs will attempt to default to the system-wide configured proxy, if possible. For instance, on Unix systems, the `http_proxy` environment variable will be picked up.

### [`CacheDirectoryPath`](https://docs.sentry.io/.md#cache-directory-path)

Specifies a local directory used for caching payloads. When this option is enabled (that is, when the directory is set), the Sentry SDK will persist envelopes locally before sending to Sentry. This configuration option is particularly useful if you expect your application to run in environments where internet connectivity is limited.

Default: not set (caching is disabled).

### [`InitCacheFlushTimeout`](https://docs.sentry.io/.md#init-cache-flush-timeout)

When caching is enabled (that is, `CacheDirectoryPath` is set), this option controls the timeout that limits how long the SDK will attempt to flush existing cache during initialization. Note that flushing the cache involves sending the payload to Sentry in a blocking operation. Setting this option to zero means that Sentry will **not** attempt to flush the cache during initialization, but instead will do so when the next payload is queued up.

The default is `1` (one) second.

### [`ShutdownTimeout`](https://docs.sentry.io/.md#shutdown-timeout)

Controls how many seconds to wait before shutting down. Sentry SDKs send events from a background queue. This queue is given a certain amount to drain pending events. The default is SDK specific but typically around two seconds. Setting this value too low may cause problems for sending events from command line applications. Setting the value too high will cause the application to block for a long time for users experiencing network connectivity problems.

## [Tracing Options](https://docs.sentry.io/.md#tracing-options)

### [`TracesSampleRate`](https://docs.sentry.io/.md#traces-sample-rate)

A number between `0` and `1`, controlling the percentage chance a given transaction will be sent to Sentry. (`0` represents 0% while `1` represents 100%.) Applies equally to all transactions created in the app. Either this or `TracesSampler` must be defined to enable tracing.

### [`TracesSampler`](https://docs.sentry.io/.md#traces-sampler)

A function responsible for determining the percentage chance a given transaction will be sent to Sentry. It will automatically be passed information about the transaction and the context in which it's being created, and must return a number between `0` (0% chance of being sent) and `1` (100% chance of being sent). Can also be used for filtering transactions, by returning 0 for those that are unwanted. Either this or `TracesSampleRate` must be defined to enable tracing.
