Skip to content

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 0 additions & 3 deletions docs/platforms/dotnet/common/logs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,3 @@ With Sentry Structured Logs, you can send text based log information from your a
## Options

<PlatformContent includePath="logs/options" />



15 changes: 15 additions & 0 deletions docs/product/explore/logs/getting-started/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ To set up Sentry Logs, use the links below for supported SDKs. After it's been s
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to put this under the ### Mobile header?

url="/platforms/dotnet/guides/maui/logs/"
/>
- <LinkWithPlatformIcon
platform="dotnet.extensions-logging"
label="Microsoft.Extensions.Logging"
url="/platforms/dotnet/guides/extensions-logging/logs/"
/>

## Upcoming SDKs

Expand Down
2 changes: 1 addition & 1 deletion platform-includes/logs/integrations/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Available integrations:
- [Microsoft.Extensions.Logging](/platforms/dotnet/guides/extensions-logging/logs/)
- [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).
11 changes: 6 additions & 5 deletions platform-includes/logs/options/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#### Experimental.EnableLogs
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.

#### Experimental.SetBeforeSendLog(Func<SentryLog, SentryLog?>)
Set to `true` in order to enable the `SentrySdk.Experimental.Logger` APIs, as well as logging integrations via the `ILogger<TCategoryName>` API.

To filter logs, or update them before they are sent to Sentry, you can use the `Experimental.SetBeforeSendLog()` option.
#### 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 =>
Expand Down Expand Up @@ -33,7 +34,7 @@ options =>
});
```

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.
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.
Expand All @@ -44,4 +45,4 @@ The log object of type `SentryLog` has the following members:
- `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: 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.
- `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.
10 changes: 6 additions & 4 deletions platform-includes/logs/setup/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
To enable logging, you need to initialize the SDK with the `Experimental.EnableLogs` option set to `true`.

<PlatformSection notSupported={["dotnet.aspnetcore"]}>
<PlatformSection notSupported={["dotnet.aspnetcore", "dotnet.aws-lambda", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}>

```csharp
SentrySdk.Init(options =>
Expand All @@ -13,7 +13,7 @@ SentrySdk.Init(options =>

</PlatformSection>

<PlatformSection supported={["dotnet.aspnetcore, dotnet.aws-lambda, dotnet.azure-functions-worker, dotnet.blazor-webassembly, dotnet.maui"]}>
<PlatformSection supported={["dotnet.aspnetcore", "dotnet.aws-lambda", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.maui"]}>

```csharp {tabTitle:Builder}
.UseSentry(options =>
Expand All @@ -39,15 +39,17 @@ SentrySdk.Init(options =>

</PlatformSection>

<PlatformSection supported={["dotnet.aspnetcore, dotnet.extensions-logging"]}>
<PlatformSection supported={["dotnet.aspnetcore", "dotnet.extensions-logging"]}>

```json {tabTitle:Configuration} {filename:appsettings.json}
{
"Sentry": {
"Dsn": "___PUBLIC_DSN___",
"Experimental": {
"EnableLogs": true
}
},
}
}
```

</PlatformSection>
16 changes: 7 additions & 9 deletions platform-includes/logs/usage/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,26 @@ During the experimental phase of the feature, we will provide more method overlo
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"]}>
<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<T>](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger-1) instances resolved through _.NET dependency injection_.
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 async Task InvokeAsync(HttpContext context)
public void Invoke()
{
logger.LogInformation("A simple log message");
await Task.Yield();
logger.LogError("A {Parameter} log message", "formatted");
await Task.Delay(TimeSpan.FromSeconds(1));
logger.LogWarning(new EventId(1, nameof(InvokeAsync)), "Message with EventId");
logger.LogWarning(new EventId(1, nameof(Invoke)), "Message with EventId");
}
}
```

The `ILogger<T>`'s _CategoryName_, as well as the `EventId` (if provided), are attached as attributes to the logs.
The `ILogger`'s _CategoryName_, as well as the `EventId` (if provided), are attached as attributes to the logs.

For more information, see article on [Logging in C# and .NET](https://learn.microsoft.com//dotnet/core/extensions/logging).
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>
Expand All @@ -60,4 +58,4 @@ Supported attribute types are:
- 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`.
Unsupported numeric types such as `ulong`, `nuint`, `decimal`, as well as all other types including `object`, are treated as `string` via `ToString()`.