Skip to content
Open
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
12 changes: 8 additions & 4 deletions docs/platforms/dotnet/guides/aspnet/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ You configure the SDK in the `Global.asax.cs`:
The snippet also turns on:

- Tracing: via <PlatformLink to="/configuration/options/#TracesSampleRate">`TracesSampleRate`</PlatformLink>
- Logs from logging integrations: via <PlatformLink to="/logs/#enablelogs">`EnableLogs`</PlatformLink>

Don't need a feature? Remove its options from the snippet.

Expand All @@ -58,7 +57,6 @@ public class MvcApplication : HttpApplication
options.Debug = true;
// Adds request URL and headers, IP and name for users, etc.
options.SendDefaultPii = true;
options.EnableLogs = true;
// Enables Tracing.
// Set to 1.0 to capture 100% of spans for tracing.
// We recommend reducing this value in production.
Expand Down Expand Up @@ -117,9 +115,15 @@ options.AddAspNet(RequestSize.Always);

## Send Logs

Use `SentrySdk.Logger` to send logs at different levels without an additional option. If you also send logs through an integration, set `EnableLogs = true` in the initialization snippet.
Use `SentrySdk.Logger` to send logs at different levels without an additional option.

If you use Serilog, NLog, or log4net, see the documented [Serilog](/platforms/dotnet/guides/serilog/), [NLog](/platforms/dotnet/guides/nlog/), and [log4net](/platforms/dotnet/guides/log4net/) integrations.
If you use Serilog, NLog, or log4net, see the documented [Serilog](/platforms/dotnet/guides/serilog/), [NLog](/platforms/dotnet/guides/nlog/), and [log4net](/platforms/dotnet/guides/log4net/) integrations. They send logs as soon as you add them. To drop logs you don't want, return `null` from <PlatformLink to="/logs/#setbeforesendlog">`SetBeforeSendLog`</PlatformLink>.

<Alert title="Changed in Version 7.0.0">

In earlier versions, the logging integrations only sent logs when `EnableLogs` was set to `true` in `SentrySdk.Init`.

</Alert>

See <PlatformLink to="/logs/">Set Up Logs</PlatformLink> for all available logging methods and formatting options.

Expand Down
40 changes: 23 additions & 17 deletions docs/platforms/dotnet/guides/azure-functions-worker/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ sdk: sentry.dotnet.azure.functions.worker
description: "Learn about Sentry's .NET integration with Azure Functions."
---

Sentry provides an integration with Azure Functions through the [Sentry.Extensions.Logging](https://www.nuget.org/packages/Sentry.Extensions.Logging) and [Sentry.OpenTelemetry.Exporter](https://www.nuget.org/packages/Sentry.OpenTelemetry.Exporter) NuGet packages.
Sentry provides an integration with Azure Functions through the [Sentry.Extensions.Hosting](https://www.nuget.org/packages/Sentry.Extensions.Hosting) and [Sentry.OpenTelemetry.Exporter](https://www.nuget.org/packages/Sentry.OpenTelemetry.Exporter) NuGet packages.

## Install

Add the Sentry dependencies to your Azure Functions application:

```shell {tabTitle:.NET Core CLI}
dotnet add package Sentry.Extensions.Logging -v {{@inject packages.version('sentry.dotnet.extensions.logging') }}
dotnet add package Sentry.Extensions.Hosting -v {{@inject packages.version('sentry.dotnet') }}
dotnet add package Sentry.OpenTelemetry.Exporter -v {{@inject packages.version('sentry.dotnet') }}
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Http
```

```powershell {tabTitle:Package Manager}
Install-Package Sentry.Extensions.Logging -Version {{@inject packages.version('sentry.dotnet.extensions.logging') }}
Install-Package Sentry.Extensions.Hosting -Version {{@inject packages.version('sentry.dotnet') }}
Install-Package Sentry.OpenTelemetry.Exporter -Version {{@inject packages.version('sentry.dotnet') }}
Install-Package OpenTelemetry.Extensions.Hosting
Install-Package OpenTelemetry.Instrumentation.Http
```

The [Sentry.Extensions.Logging](/platforms/dotnet/guides/extensions-logging/) package also provides access to the `ILogger<T>` integration and the other core features available in the [Sentry](/platforms/dotnet/) SDK.
This package extends [Sentry.Extensions.Logging](/platforms/dotnet/guides/extensions-logging/). This means you also get the `ILogger<T>` integration and the other core features available in the [Sentry](/platforms/dotnet/) SDK.

## Configure

**Error Monitoring** starts working as soon as you initialize the SDK by calling `AddSentry` when configuring logging. **Logs** sent through `Microsoft.Extensions.Logging` need `EnableLogs = true`, and **Application Metrics** need no extra configuration. You'll learn how to send them in [Send Logs](#send-logs) and [Send Application Metrics](#send-application-metrics-).
**Error Monitoring** starts working as soon as you call `UseSentry` on the host builder. **Logs** sent through `Microsoft.Extensions.Logging` need `EnableLogs = true`, and **Application Metrics** need no extra configuration. You'll learn how to send them in [Send Logs](#send-logs) and [Send Application Metrics](#send-application-metrics-).

`UseSentry` starts Sentry when the host is built and closes it when the host stops. It also adds Sentry's `ILogger` integration, which captures `LogError` and higher as events.

This guide also enables **Tracing** through OpenTelemetry. Sentry's OTLP exporter is added to the OpenTelemetry `TracerProviderBuilder`, and `UseOtlp()` tells the Sentry SDK to use OpenTelemetry for tracing.

Expand All @@ -42,11 +44,17 @@ Don't need a feature? Remove its options from the snippet.
```csharp
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Trace;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.UseSentry(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.EnableLogs = true;
options.UseOtlp(); // <-- Configure Sentry to use OpenTelemetry for tracing
options.Debug = true;
})
.ConfigureServices(services =>
{
services.AddOpenTelemetry().WithTracing(builder =>
Expand All @@ -56,24 +64,22 @@ var host = new HostBuilder()
.AddHttpClientInstrumentation(); // From OpenTelemetry.Instrumentation.Http, instruments outgoing HTTP requests
});
})
.ConfigureLogging(logging =>
{
logging.AddSentry(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.EnableLogs = true;
options.UseOtlp(); // <-- Configure Sentry to use OpenTelemetry for tracing
options.Debug = true;
});
})
.Build();

await host.RunAsync();
```

Apps that use the ASP.NET Core integration, with `ConfigureFunctionsWebApplication()` instead of `ConfigureFunctionsWorkerDefaults()`, call the same `UseSentry`, not the one from `Sentry.AspNetCore`.

<Alert title="Changed in Version 7.0.0">

In earlier versions, there was no `Sentry.Extensions.Hosting` package. On those versions, install `Sentry.Extensions.Logging` and pass the same options to `logging.AddSentry(options => ...)` in `ConfigureLogging`, instead of calling `UseSentry`.

</Alert>

## Send Logs

Use `Microsoft.Extensions.Logging` to send structured logs from your application. Set `EnableLogs = true` in your Sentry initialization.
Use `Microsoft.Extensions.Logging` to send structured logs from your application. Set `EnableLogs = true` in the options you pass to `UseSentry`.

See [Microsoft.Extensions.Logging](/platforms/dotnet/guides/extensions-logging/) for setup details.

Expand Down
9 changes: 1 addition & 8 deletions docs/platforms/dotnet/guides/blazor-webassembly/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ This package extends [Sentry.Extensions.Logging](/platforms/dotnet/guides/extens

This guide also enables **Tracing**.


Sentry integration with Blazor WebAssembly is done by calling `.UseSentry()` and specifying the options, for example:


Sentry integration with Blazor WebAssembly is done by calling `.UseSentry()` and specifying the options. `UseSentry` also adds Sentry's `ILogger` integration, which captures `LogError` and higher as events.

The snippet also turns on:

Expand All @@ -39,7 +36,6 @@ Don't need a feature? Remove its options from the snippet.

```csharp
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Logging;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.UseSentry(options =>
Expand All @@ -56,9 +52,6 @@ builder.UseSentry(options =>
options.TracesSampleRate = 0.1;
});

// Captures logError and higher as events
builder.Logging.AddSentry(o => o.InitializeSdk = false);

await builder.Build().RunAsync();
```

Expand Down
74 changes: 61 additions & 13 deletions docs/platforms/dotnet/guides/log4net/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ og_image: /og-images/platforms-dotnet-guides-log4net.gif

Sentry provides an integration with log4net through the [Sentry.Log4Net NuGet package](https://www.nuget.org/packages/Sentry.Log4Net).

Without any code change, this package is able to initialize the Sentry SDK and capture events while including additional properties like `Exception` data and more.
The `SentryAppender` captures log events as Sentry events and breadcrumbs, including additional properties like `Exception` data and more.

Additionally, when enabled, log messages are sent to Sentry as [Structured Logs](logs/).
Log messages are also sent to Sentry as [Structured Logs](logs/).

## Install

Expand All @@ -27,35 +27,83 @@ This package extends `Sentry` main SDK. That means besides the log4net `Appender

## Configure

Once the log4net integration package is installed in your project, you can modify your configuration file to add the appender.
Initialize Sentry first, with `SentrySdk.Init` or a host integration such as [ASP.NET Core](/platforms/dotnet/guides/aspnetcore/):

```csharp
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
});
```

Then add the appender to your log4net configuration.
This can be done, for example, via the `app.config` for console and desktop apps or `web.config` in case of ASP.NET.


```xml {filename:app.config}
<appender name="SentryAppender" type="Sentry.Log4Net.SentryAppender, Sentry.Log4Net">
<Dsn value="___PUBLIC_DSN___"/>
<!--Sends the log event Identity value as the user-->
<SendIdentity value="true" />
<threshold value="INFO" />
</appender>
```

<Alert>
Other [SDK options](/platforms/dotnet/guides/log4net/configuration/options/) can also be configured when calling `SentrySdk.Init`.

Only a subset of the options are exposed via the log4net appender configuration. If you wish to access an [SDK option](/platforms/dotnet/configuration/options/) which is not listed below, you'll need to initialize the SDK via [SentrySdk.Init](/platforms/dotnet/) instead of doing it via this integration as described below.
The appender drops log events while Sentry isn't initialized. If a DSN can be found anyway, for example in the `SENTRY_DSN` environment variable, the appender also writes a warning to standard error the first time it drops an event. To fix it, initialize Sentry at startup as shown above.

</Alert>
### Apps That Call UseSentry

### SendIdentity
<Include name="dotnet-usesentry-logging/log4net" />

In the example above, the `SendIdentity` flag was switched on. The SDK then will take the log4net `Identity` value and report to Sentry as the user's id.
### Capturing Startup Crashes

`UseSentry` starts Sentry when the host is built or, in ASP.NET Core, when the app starts running. If the app crashes before then, the appender drops the crash.

To send these crashes, also call `SentrySdk.Init` before you create the builder, and log the crash if startup fails:

```csharp {filename:Program.cs}
using log4net;

### DSN
using var sentry = SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
});

Also in the example above, you can find the DSN being set. That will instruct the `SentryAppender` to initialize the SDK.
var logger = LogManager.GetLogger(typeof(Program));

This is only one of the ways to initialize the SDK. If you wish to configure the SDK programmatically, you could **leave the DSN out** from the appender configuration section. The SDK needs to be initialized only **once** and since other integrations (like ASP.NET) are also able to initialize the SDK, you only need to pass the DSN to one of these integrations.
One common case to not add the DSN to the XML configuration file (which would initialize it via the log4net integration) is to have full access to the [SDK option](/platforms/dotnet/guides/log4net/configuration/options/).
try
{
var builder = WebApplication.CreateBuilder(args);
builder.UseSentry(options =>
{
options.Dsn = "___PUBLIC_DSN___";
});

var app = builder.Build();
app.Run();
}
catch (Exception ex)
{
logger.Fatal("Application terminated unexpectedly", ex);
throw;
}
finally
{
LogManager.Shutdown();
}
```

The two initializations don't share anything:

- `SentrySdk.Init` runs before the app reads `appsettings.json`, so set its DSN in code or with the `SENTRY_DSN` environment variable.
- `UseSentry` doesn't reuse those options. Give it a DSN as well, in its callback or the `Sentry` section.
- Once `UseSentry` starts Sentry, it replaces the first initialization. Crashes before then are sent with the first initialization's options, such as its release and environment. Breadcrumbs logged before then aren't carried over.
- Crashes after then, such as a port that's already in use, are sent by `UseSentry`. `logger.Fatal` doesn't send them a second time.

### SendIdentity

In the example above, the `SendIdentity` flag was switched on. The SDK then will take the log4net `Identity` value and report to Sentry as the user's id.

## Verify

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,32 @@ For more information on how to dynamically set event data via `NLog.config`, see

</Alert>

The SDK can be configured via `NLog.config` XML file:
Call `SentrySdk.Init` to initialize the SDK first. SDK options, such as the environment and release, go here:

```csharp
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.Environment = "Development";
options.Release = "my-project-name@2.3.12";
options.AttachStacktrace = true;
options.SendDefaultPii = true; // Send Personal Identifiable information like the username of the user logged in to the device
options.ShutdownTimeout = TimeSpan.FromSeconds(5);
});
```

```fsharp
SentrySdk.Init(fun options ->
options.Dsn <- "___PUBLIC_DSN___"
options.Environment <- "Development"
options.Release <- "my-project-name@2.3.12"
options.AttachStacktrace <- true
options.SendDefaultPii <- true // Send Personal Identifiable information like the username of the user logged in to the device
options.ShutdownTimeout <- TimeSpan.FromSeconds 5.0)
|> ignore
```

Then configure the Sentry target in an `NLog.config` XML file:


```xml {tabTitle:NLog.config}
Expand All @@ -26,29 +51,15 @@ The SDK can be configured via `NLog.config` XML file:
<target
xsi:type="Sentry"
name="sentry"
dsn="___PUBLIC_DSN___"
environment="Development"
release="my-project-name@2.3.12"
includeEventProperties="True"
layout="${message}"
breadcrumbLayout="${message}"
minimumBreadcrumbLevel="Debug"
ignoreEventsWithNoException="False"
includeEventDataOnBreadcrumbs="False"
includeEventDataOnBreadcrumbs="True"
includeEventPropertiesAsTags="True"
minimumEventLevel="Error">

<!-- Advanced options can be configured here-->
<options
sendDefaultPii="true"
attachStacktrace="false"
shutdownTimeoutSeconds="5"
debug="false"
>
<!--Advanced options can be specified as attributes or elements-->
<includeEventDataOnBreadcrumbs>true</includeEventDataOnBreadcrumbs>
</options>

<!-- Optionally specify user properties via NLog
(here using MappedDiagnosticsLogicalContext as an example) -->
<user id="${mdlc:item=id}"
Expand Down Expand Up @@ -77,10 +88,9 @@ The SDK can be configured via `NLog.config` XML file:
</nlog>
```

Or the SDK can be configured via code:
Or configure the target in code:

```csharp
// Other overloads exist, for example, configure the SDK with only the DSN or no parameters at all.
LogManager.Configuration.AddSentry(o =>
{
o.Layout = "${message}";
Expand All @@ -89,15 +99,7 @@ LogManager.Configuration.AddSentry(o =>
o.MinimumBreadcrumbLevel = LogLevel.Debug; // Debug and higher are stored as breadcrumbs (default is Info)
o.MinimumEventLevel = LogLevel.Error; // Error and higher is sent as event (default is Error)

// If DSN is not set, the SDK will look for an environment variable called SENTRY_DSN. If
// nothing is found, SDK is disabled.
o.Dsn = DsnSample;

o.AttachStacktrace = true;
o.SendDefaultPii = true; // Send Personal Identifiable information like the username of the user logged in to the device

o.IncludeEventDataOnBreadcrumbs = true; // Optionally include event properties with breadcrumbs
o.ShutdownTimeoutSeconds = 5;

//Optionally specify user properties via NLog (here using MappedDiagnosticsLogicalContext as an example)
o.User = new SentryNLogUser
Expand All @@ -119,7 +121,6 @@ LogManager.Configuration.AddSentry(o =>
```

```fsharp
// Other overloads exist, for example, configure the SDK with only the DSN or no parameters at all.
LogManager.Configuration.AddSentry(
fun o ->
o.Layout <- "${message}"
Expand All @@ -128,15 +129,7 @@ LogManager.Configuration.AddSentry(
o.MinimumBreadcrumbLevel <- LogLevel.Debug // Debug and higher are stored as breadcrumbs (default is Info)
o.MinimumEventLevel <- LogLevel.Error // Error and higher is sent as event (default is Error)

// If DSN is not set, the SDK will look for an environment variable called SENTRY_DSN. If
// nothing is found, SDK is disabled.
o.Dsn <- DsnSample

o.AttachStacktrace <- true
o.SendDefaultPii <- true // Send Personal Identifiable information like the username of the user logged in to the device

o.IncludeEventDataOnBreadcrumbs <- true // Optionally include event properties with breadcrumbs
o.ShutdownTimeoutSeconds <- 5

//Optionally specify user properties via NLog (here using MappedDiagnosticsLogicalContext as an example)
o.User <-
Expand Down
Loading
Loading