-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs(dotnet/logs): add Logs for the .NET SDK #14583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Flash0ver
wants to merge
5
commits into
master
Choose a base branch
from
docs/add-dotnet-logs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7be2901
docs(dotnet/logs): add Logs for the .NET SDK
Flash0ver 4f18481
docs(dotnet/logs): remove redundant additions
Flash0ver a1cc7da
docs(dotnet/logs): fix invalid chevrons in link text
Flash0ver ab395c8
docs(dotnet/logs): add platforms to getting started
Flash0ver 9cdc978
docs(dotnet/logs): fix bad PlatformSection syntax
Flash0ver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: Set Up Logs | ||
sidebar_title: Logs | ||
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry." | ||
sidebar_order: 5600 | ||
notSupported: | ||
- dotnet.google-cloud-functions | ||
- dotnet.log4net | ||
- dotnet.nlog | ||
- dotnet.serilog | ||
- dotnet.xamarin | ||
--- | ||
|
||
<Include name="feature-stage-beta-logs.mdx" /> | ||
|
||
With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes. | ||
|
||
## Requirements | ||
|
||
<PlatformContent includePath="logs/requirements" /> | ||
|
||
## Setup | ||
|
||
<PlatformContent includePath="logs/setup" /> | ||
|
||
## Usage | ||
|
||
<PlatformContent includePath="logs/usage" /> | ||
|
||
## Integrations | ||
|
||
<PlatformContent includePath="logs/integrations" /> | ||
|
||
## Options | ||
|
||
<PlatformContent includePath="logs/options" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,6 +246,29 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s | |
|
||
- <LinkWithPlatformIcon platform="rust" label="Rust" url="/platforms/rust/logs/" /> | ||
|
||
### .NET | ||
|
||
- <LinkWithPlatformIcon | ||
platform="dotnet" | ||
label=".NET" | ||
url="/platforms/dotnet/logs/" | ||
/> | ||
- <LinkWithPlatformIcon | ||
platform="dotnet.aspnetcore" | ||
label="ASP.NET Core" | ||
url="/platforms/dotnet/guides/aspnetcore/logs/" | ||
/> | ||
- <LinkWithPlatformIcon | ||
platform="dotnet.maui" | ||
label="MAUI" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to put this under the |
||
url="/platforms/dotnet/guides/maui/logs/" | ||
/> | ||
- <LinkWithPlatformIcon | ||
platform="dotnet.extensions-logging" | ||
label="Microsoft.Extensions.Logging" | ||
url="/platforms/dotnet/guides/extensions-logging/logs/" | ||
/> | ||
|
||
## Upcoming SDKs | ||
|
||
We're actively working on adding Log functionality to additional SDKs. Check out these GitHub issues for the latest updates: | ||
|
@@ -260,11 +283,6 @@ We're actively working on adding Log functionality to additional SDKs. Check out | |
label="Elixir" | ||
url="https://github.com/getsentry/sentry-elixir/issues/886" | ||
/> | ||
- <LinkWithPlatformIcon | ||
platform="dotnet" | ||
label=".NET" | ||
url="https://github.com/getsentry/sentry-dotnet/issues/4132" | ||
/> | ||
- <LinkWithPlatformIcon | ||
platform="php.symfony" | ||
label="Symfony" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Available integrations: | ||
- [ASP.NET Core](/platforms/dotnet/guides/aspnetcore/logs/) | ||
- [.NET Multi-platform App UI (.NET MAUI)](/platforms/dotnet/guides/maui/logs/) | ||
- [Microsoft.Extensions.Logging](/platforms/dotnet/guides/extensions-logging/logs/) | ||
|
||
If there's an integration you would like to see, open a [new issue on GitHub](https://github.com/getsentry/sentry-dotnet/issues/new/choose). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#### Experimental.EnableLogs | ||
|
||
Set to `true` in order to enable the `SentrySdk.Experimental.Logger` APIs, as well as logging integrations via the `ILogger<TCategoryName>` API. | ||
|
||
#### Experimental.SetBeforeSendLog | ||
|
||
To filter logs, or update them before they are sent to Sentry, you can use the `Experimental.SetBeforeSendLog(Func<SentryLog, SentryLog?>)` option. | ||
|
||
```csharp | ||
options => | ||
{ | ||
options.Dsn = "___PUBLIC_DSN___"; | ||
options.Experimental.EnableLogs = true; | ||
// a callback that is invoked before sending a log to Sentry | ||
options.Experimental.SetBeforeSendLog(static log => | ||
{ | ||
// filter out all info logs | ||
if (log.Level is SentryLogLevel.Info) | ||
{ | ||
return null; | ||
} | ||
|
||
// filter out logs based on some attribute they have | ||
if (log.TryGetAttribute("suppress", out var attribute) && attribute is true) | ||
{ | ||
return null; | ||
} | ||
|
||
// set a custom attribute for all other logs sent to Sentry | ||
log.SetAttribute("my.attribute", "value"); | ||
|
||
return log; | ||
}); | ||
}); | ||
``` | ||
|
||
The callback function set via `Experimental.SetBeforeSendLog(Func<SentryLog, SentryLog?>)` receives a log object, and should return the log object if you want it to be sent to Sentry, or `null` if you want to discard it. | ||
|
||
The log object of type `SentryLog` has the following members: | ||
- `Timestamp` Property: (`DateTimeOffset`) The timestamp of the log. | ||
- `TraceId` Property: (`SentryId`) The trace id of the log. | ||
- `Level` Property: (`SentryLogLevel`) The severity level of the log. Either `Trace`, `Debug`, `Info`, `Warning`, `Error`, or `Fatal`. | ||
- `Message` Property: (`string`) The formatted log message. | ||
- `Template` Property: (`string?`) The parameterized template string. | ||
- `Parameters` Property: (`ImmutableArray<KeyValuePair<string, object>>`) The parameters to the template string. | ||
- `ParentSpanId` Property: (`SpanId?`) The span id of the span that was active when the log was collected. | ||
- `TryGetAttribute(string key, out object value)` Method: Gets the attribute value associated with the specified key. Returns `true` if the log contains an attribute with the specified key and it's value is not `null`, otherwise `false`. | ||
- `SetAttribute(string key, object value)` Method: Sets a key-value pair of data attached to the log. Supported types are `string`, `bool`, integers up to a size of 64-bit signed, and floating-point numbers up to a size of 64-bit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Logs for .NET are supported in Sentry .NET SDK version `5.14.0` and above. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
To enable logging, you need to initialize the SDK with the `Experimental.EnableLogs` option set to `true`. | ||
|
||
<PlatformSection notSupported={["dotnet.aspnetcore", "dotnet.aws-lambda", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}> | ||
|
||
```csharp | ||
SentrySdk.Init(options => | ||
{ | ||
options.Dsn = "___PUBLIC_DSN___"; | ||
// Enable logs to be sent to Sentry | ||
options.Experimental.EnableLogs = true; | ||
}); | ||
``` | ||
|
||
</PlatformSection> | ||
|
||
<PlatformSection supported={["dotnet.aspnetcore", "dotnet.aws-lambda", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.maui"]}> | ||
|
||
```csharp {tabTitle:Builder} | ||
.UseSentry(options => | ||
{ | ||
options.Dsn = "___PUBLIC_DSN___"; | ||
// Enable logs to be sent to Sentry | ||
options.Experimental.EnableLogs = true; | ||
}); | ||
``` | ||
|
||
</PlatformSection> | ||
|
||
<PlatformSection supported={["dotnet.extensions-logging"]}> | ||
|
||
```csharp {tabTitle:Factory} | ||
.AddSentry(options => | ||
{ | ||
options.Dsn = "___PUBLIC_DSN___"; | ||
// Enable logs to be sent to Sentry | ||
options.Experimental.EnableLogs = true; | ||
}); | ||
``` | ||
|
||
</PlatformSection> | ||
|
||
<PlatformSection supported={["dotnet.aspnetcore", "dotnet.extensions-logging"]}> | ||
|
||
```json {tabTitle:Configuration} {filename:appsettings.json} | ||
{ | ||
"Sentry": { | ||
"Dsn": "___PUBLIC_DSN___", | ||
"Experimental": { | ||
"EnableLogs": true | ||
} | ||
} | ||
} | ||
``` | ||
|
||
</PlatformSection> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `SentrySdk.Experimental.Logger` APIs. | ||
|
||
The `SentrySdk.Experimental.Logger` instance exposes six methods that you can use to log messages at different log levels: `Trace`, `Debug`, `Info`, `Warning`, `Error`, and `Fatal`. | ||
|
||
These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column. | ||
|
||
```csharp | ||
SentrySdk.Experimental.Logger.LogInfo("A simple log message"); | ||
SentrySdk.Experimental.Logger.LogError("A {0} log message", ["formatted"]); | ||
``` | ||
|
||
<Alert title="Note"> | ||
During the experimental phase of the feature, we will provide more method overloads for convenient invocation in common scenarios. | ||
Additionally, we may provide method overloads that are not based on _composite format strings_, but on _interpolated strings_. | ||
</Alert> | ||
|
||
<PlatformSection supported={["dotnet.aspnetcore", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}> | ||
|
||
Additionally, when enabled and initialized, the SDK registers a [ILoggerProvider](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.iloggerprovider) | ||
that allows sending logs to Sentry via [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger-1) instances resolved through _.NET dependency injection_. | ||
|
||
```csharp | ||
public sealed class MyService(ILogger<MyService> logger) | ||
{ | ||
public void Invoke() | ||
{ | ||
logger.LogInformation("A simple log message"); | ||
logger.LogError("A {Parameter} log message", "formatted"); | ||
logger.LogWarning(new EventId(1, nameof(Invoke)), "Message with EventId"); | ||
} | ||
} | ||
``` | ||
|
||
The `ILogger`'s _CategoryName_, as well as the `EventId` (if provided), are attached as attributes to the logs. | ||
|
||
For more information, see the article on [Logging in C# and .NET](https://learn.microsoft.com//dotnet/core/extensions/logging). | ||
Sentry Structured Logs also work with [High-performance logging in .NET](https://learn.microsoft.com/dotnet/core/extensions/high-performance-logging) and [Compile-time logging source generation](https://learn.microsoft.com/dotnet/core/extensions/logger-message-generator) alike. | ||
|
||
</PlatformSection> | ||
|
||
The SDK automatically provides a set of default attributes attached to your logs. | ||
Additionally, you can attach custom attributes via a delegate. | ||
|
||
```csharp | ||
SentrySdk.Experimental.Logger.LogWarning("A log message with additional attributes.", [], static log => | ||
{ | ||
log.SetAttribute("my.attribute", "value"); | ||
}); | ||
``` | ||
|
||
<Alert title="Note"> | ||
Please note that we will revise the API shape to set custom attributes during the experimental phase of the feature. | ||
</Alert> | ||
|
||
Supported attribute types are: | ||
- Textual: `string` and `char` | ||
- Logical: `bool` | ||
- Integral: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long` and `nint` | ||
- Floating-point: `float` and `double` | ||
|
||
Unsupported numeric types such as `ulong`, `nuint`, `decimal`, as well as all other types including `object`, are treated as `string` via `ToString()`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.