Skip to content

Commit 7be2901

Browse files
committed
docs(dotnet/logs): add Logs for the .NET SDK
1 parent ff34007 commit 7be2901

File tree

7 files changed

+223
-5
lines changed

7 files changed

+223
-5
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Set Up Logs
3+
sidebar_title: Logs
4+
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry."
5+
sidebar_order: 5600
6+
notSupported:
7+
- dotnet.google-cloud-functions
8+
- dotnet.log4net
9+
- dotnet.nlog
10+
- dotnet.serilog
11+
- dotnet.xamarin
12+
---
13+
14+
<Include name="feature-stage-beta-logs.mdx" />
15+
16+
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.
17+
18+
## Requirements
19+
20+
<PlatformContent includePath="logs/requirements" />
21+
22+
## Setup
23+
24+
<PlatformContent includePath="logs/setup" />
25+
26+
## Usage
27+
28+
<PlatformContent includePath="logs/usage" />
29+
30+
## Integrations
31+
32+
<PlatformContent includePath="logs/integrations" />
33+
34+
## Options
35+
36+
<PlatformContent includePath="logs/options" />
37+
38+
39+
## Feedback
40+
Sentry Structured Logs is currently in Beta and under active development.
41+
During the experimental phase of support in Sentry's .NET SDK, are are looking forward to your general feedback, both praise and problems, through _Give Feedback_ on Sentry, our [Discord server](https://discord.gg/sentry), or this [GitHub discussion](TODO).
42+
Should you experience bugs or have particular feature request, please open a [new issue on GitHub](https://github.com/getsentry/sentry-dotnet/issues/new/choose).
43+
44+
## Known issues and upcoming improvement
45+
This [GitHub issue](TODO) tracks all bugs that we are currently fixing and improvement that we will implement during the experimental phase of the feature.

docs/product/explore/logs/getting-started/index.mdx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s
246246

247247
- <LinkWithPlatformIcon platform="rust" label="Rust" url="/platforms/rust/logs/" />
248248

249+
### .NET
250+
251+
- <LinkWithPlatformIcon
252+
platform="dotnet"
253+
label=".NET"
254+
url="/platforms/dotnet/logs/"
255+
/>
256+
249257
## Upcoming SDKs
250258

251259
We're actively working on adding Log functionality to additional SDKs. Check out these GitHub issues for the latest updates:
@@ -260,11 +268,6 @@ We're actively working on adding Log functionality to additional SDKs. Check out
260268
label="Elixir"
261269
url="https://github.com/getsentry/sentry-elixir/issues/886"
262270
/>
263-
- <LinkWithPlatformIcon
264-
platform="dotnet"
265-
label=".NET"
266-
url="https://github.com/getsentry/sentry-dotnet/issues/4132"
267-
/>
268271
- <LinkWithPlatformIcon
269272
platform="php.symfony"
270273
label="Symfony"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Available integrations:
2+
- [Microsoft.Extensions.Logging](/platforms/dotnet/guides/extensions-logging/logs/)
3+
- [ASP.NET Core](/platforms/dotnet/guides/aspnetcore/logs/)
4+
- [.NET Multi-platform App UI (.NET MAUI)](/platforms/dotnet/guides/maui/logs/)
5+
6+
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).
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#### Experimental.EnableLogs
2+
Set to `true` in order to enable the `SentrySdk.Experimental.Logger` APIs, as well as logging integrations via the [ILogger<T>](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger-1) API.
3+
4+
#### Experimental.SetBeforeSendLog(Func<SentryLog, SentryLog?>)
5+
6+
To filter logs, or update them before they are sent to Sentry, you can use the `Experimental.SetBeforeSendLog()` option.
7+
8+
```csharp
9+
options =>
10+
{
11+
options.Dsn = "___PUBLIC_DSN___";
12+
options.Experimental.EnableLogs = true;
13+
// a callback that is invoked before sending a log to Sentry
14+
options.Experimental.SetBeforeSendLog(static log =>
15+
{
16+
// filter out all info logs
17+
if (log.Level is SentryLogLevel.Info)
18+
{
19+
return null;
20+
}
21+
22+
// filter out logs based on some attribute they have
23+
if (log.TryGetAttribute("suppress", out var attribute) && attribute is true)
24+
{
25+
return null;
26+
}
27+
28+
// set a custom attribute for all other logs sent to Sentry
29+
log.SetAttribute("my.attribute", "value");
30+
31+
return log;
32+
});
33+
});
34+
```
35+
36+
The callback function set via `Experimental.SetBeforeSendLog()` 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.
37+
38+
The log object of type `SentryLog` has the following members:
39+
- `Timestamp` Property: (`DateTimeOffset`) The timestamp of the log.
40+
- `TraceId` Property: (`SentryId`) The trace id of the log.
41+
- `Level` Property: (`SentryLogLevel`) The severity level of the log. Either `Trace`, `Debug`, `Info`, `Warning`, `Error`, or `Fatal`.
42+
- `Message` Property: (`string`) The formatted log message.
43+
- `Template` Property: (`string?`) The parameterized template string.
44+
- `Parameters` Property: (`ImmutableArray<KeyValuePair<string, object>>`) The parameters to the template string.
45+
- `ParentSpanId` Property: (`SpanId?`) The span id of the span that was active when the log was collected.
46+
- `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`.
47+
- `SetAttribute(string key, object value)` Method: Set 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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Logs for .NET are supported in Sentry .NET SDK version `5.14.0` and above.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
To enable logging, you need to initialize the SDK with the `Experimental.EnableLogs` option set to `true`.
2+
3+
<PlatformSection notSupported={["dotnet.aspnetcore"]}>
4+
5+
```csharp
6+
SentrySdk.Init(options =>
7+
{
8+
options.Dsn = "___PUBLIC_DSN___";
9+
// Enable logs to be sent to Sentry
10+
options.Experimental.EnableLogs = true;
11+
});
12+
```
13+
14+
</PlatformSection>
15+
16+
<PlatformSection supported={["dotnet.aspnetcore, dotnet.aws-lambda, dotnet.azure-functions-worker, dotnet.blazor-webassembly, dotnet.maui"]}>
17+
18+
```csharp {tabTitle:Builder}
19+
.UseSentry(options =>
20+
{
21+
options.Dsn = "___PUBLIC_DSN___";
22+
// Enable logs to be sent to Sentry
23+
options.Experimental.EnableLogs = true;
24+
});
25+
```
26+
27+
</PlatformSection>
28+
29+
<PlatformSection supported={["dotnet.extensions-logging"]}>
30+
31+
```csharp {tabTitle:Factory}
32+
.AddSentry(options =>
33+
{
34+
options.Dsn = "___PUBLIC_DSN___";
35+
// Enable logs to be sent to Sentry
36+
options.Experimental.EnableLogs = true;
37+
});
38+
```
39+
40+
</PlatformSection>
41+
42+
<PlatformSection supported={["dotnet.aspnetcore, dotnet.extensions-logging"]}>
43+
44+
```json {tabTitle:Configuration} {filename:appsettings.json}
45+
"Sentry": {
46+
"Dsn": "___PUBLIC_DSN___",
47+
"Experimental": {
48+
"EnableLogs": true
49+
}
50+
},
51+
```
52+
53+
</PlatformSection>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `SentrySdk.Experimental.Logger` APIs.
2+
3+
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`.
4+
5+
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.
6+
7+
```csharp
8+
SentrySdk.Experimental.Logger.LogInfo("A simple log message");
9+
SentrySdk.Experimental.Logger.LogError("A {0} log message", ["formatted"]);
10+
```
11+
12+
<Alert title="Note">
13+
During the experimental phase of the feature, we will provide more method overloads for convenient invocation in common scenarios.
14+
Additionally, we may provide method overloads that are not based on _composite format strings_, but on _interpolated strings_.
15+
</Alert>
16+
17+
<PlatformSection supported={["dotnet.aspnetcore, dotnet.azure-functions-worker, dotnet.blazor-webassembly, dotnet.extensions-logging, dotnet.maui"]}>
18+
19+
Additionally, when enabled and initialized, the SDK registers a [ILoggerProvider](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.iloggerprovider)
20+
that allows sending logs to Sentry via [ILogger<T>](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger-1) instances resolved through _.NET dependency injection_.
21+
22+
```csharp
23+
public sealed class MyService(ILogger<MyService> logger)
24+
{
25+
public async Task InvokeAsync(HttpContext context)
26+
{
27+
logger.LogInformation("A simple log message");
28+
await Task.Yield();
29+
logger.LogError("A {Parameter} log message", "formatted");
30+
await Task.Delay(TimeSpan.FromSeconds(1));
31+
logger.LogWarning(new EventId(1, nameof(InvokeAsync)), "Message with EventId");
32+
}
33+
}
34+
```
35+
36+
The `ILogger<T>`'s _CategoryName_, as well as the `EventId` (if provided), are attached as attributes to the logs.
37+
38+
For more information, see article on [Logging in C# and .NET](https://learn.microsoft.com//dotnet/core/extensions/logging).
39+
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.
40+
41+
</PlatformSection>
42+
43+
The SDK automatically provides a set of default attributes attached to your logs.
44+
Additionally, you can attach custom attributes via a delegate.
45+
46+
```csharp
47+
SentrySdk.Experimental.Logger.LogWarning("A log message with additional attributes.", [], static log =>
48+
{
49+
log.SetAttribute("my.attribute", "value");
50+
});
51+
```
52+
53+
<Alert title="Note">
54+
Please note that we will revise the API shape to set custom attributes during the experimental phase of the feature.
55+
</Alert>
56+
57+
Supported attribute types are:
58+
- Textual: `string` and `char`
59+
- Logical: `bool`
60+
- Integral: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long` and `nint`
61+
- Floating-point: `float` and `double`
62+
63+
Unsupported numeric types such as `ulong`, `nuint`, `decimal`, as well as all other types including `object`, are treated as `string` via `ToString`.

0 commit comments

Comments
 (0)