Skip to content

Commit 0a08754

Browse files
authored
feat(logs): rework method overloads of Structured Logger (#4451)
1 parent f7a83af commit 0a08754

9 files changed

+391
-189
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Experimental _Structured Logs_:
8+
- Redesign SDK Logger APIs to allow usage of `params` ([#4451](https://github.com/getsentry/sentry-dotnet/pull/4451))
89
- Shorten the `key` names of `Microsoft.Extensions.Logging` attributes ([#4450](https://github.com/getsentry/sentry-dotnet/pull/4450))
910

1011
### Fixes

samples/Sentry.Samples.Console.Basic/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// This option tells Sentry to capture 100% of traces. You still need to start transactions and spans.
3838
options.TracesSampleRate = 1.0;
3939

40-
// This option enables Sentry Logs created via SentrySdk.Logger.
40+
// This option enables Sentry Logs created via SentrySdk.Experimental.Logger.
4141
options.Experimental.EnableLogs = true;
4242
options.Experimental.SetBeforeSendLog(static log =>
4343
{
@@ -94,7 +94,8 @@ async Task SecondFunction()
9494
SentrySdk.CaptureException(exception);
9595
span.Finish(exception);
9696

97-
SentrySdk.Experimental.Logger.LogError("Error with message: {0}", [exception.Message], static log => log.SetAttribute("method", nameof(SecondFunction)));
97+
SentrySdk.Experimental.Logger.LogError(static log => log.SetAttribute("method", nameof(SecondFunction)),
98+
"Error with message: {0}", exception.Message);
9899
}
99100

100101
span.Finish();
@@ -108,7 +109,8 @@ async Task ThirdFunction()
108109
// Simulate doing some work
109110
await Task.Delay(100);
110111

111-
SentrySdk.Experimental.Logger.LogFatal("Crash imminent!", [], static log => log.SetAttribute("suppress", true));
112+
SentrySdk.Experimental.Logger.LogFatal(static log => log.SetAttribute("suppress", true),
113+
"Crash imminent!");
112114

113115
// This is an example of an unhandled exception. It will be captured automatically.
114116
throw new InvalidOperationException("Something happened that crashed the app!");
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using Sentry.Infrastructure;
2+
3+
namespace Sentry;
4+
5+
public abstract partial class SentryStructuredLogger
6+
{
7+
/// <summary>
8+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Trace"/>, when enabled and sampled.
9+
/// <para>This API is experimental and it may change in the future.</para>
10+
/// </summary>
11+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
12+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
13+
[Experimental(DiagnosticId.ExperimentalFeature)]
14+
public void LogTrace(string template, params object[] parameters)
15+
{
16+
CaptureLog(SentryLogLevel.Trace, template, parameters, null);
17+
}
18+
19+
/// <summary>
20+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Trace"/>, when enabled and sampled.
21+
/// <para>This API is experimental and it may change in the future.</para>
22+
/// </summary>
23+
/// <param name="configureLog">A delegate to set attributes on the <see cref="SentryLog"/>. When the delegate throws an <see cref="Exception"/> during invocation, the log will not be captured.</param>
24+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
25+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
26+
[Experimental(DiagnosticId.ExperimentalFeature)]
27+
public void LogTrace(Action<SentryLog> configureLog, string template, params object[] parameters)
28+
{
29+
CaptureLog(SentryLogLevel.Trace, template, parameters, configureLog);
30+
}
31+
32+
/// <summary>
33+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Debug"/>, when enabled and sampled.
34+
/// <para>This API is experimental and it may change in the future.</para>
35+
/// </summary>
36+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
37+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
38+
[Experimental(DiagnosticId.ExperimentalFeature)]
39+
public void LogDebug(string template, params object[] parameters)
40+
{
41+
CaptureLog(SentryLogLevel.Debug, template, parameters, null);
42+
}
43+
44+
/// <summary>
45+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Debug"/>, when enabled and sampled.
46+
/// <para>This API is experimental and it may change in the future.</para>
47+
/// </summary>
48+
/// <param name="configureLog">A delegate to set attributes on the <see cref="SentryLog"/>. When the delegate throws an <see cref="Exception"/> during invocation, the log will not be captured.</param>
49+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
50+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
51+
[Experimental(DiagnosticId.ExperimentalFeature)]
52+
public void LogDebug(Action<SentryLog> configureLog, string template, params object[] parameters)
53+
{
54+
CaptureLog(SentryLogLevel.Debug, template, parameters, configureLog);
55+
}
56+
57+
/// <summary>
58+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Info"/>, when enabled and sampled.
59+
/// <para>This API is experimental and it may change in the future.</para>
60+
/// </summary>
61+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
62+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
63+
[Experimental(DiagnosticId.ExperimentalFeature)]
64+
public void LogInfo(string template, params object[] parameters)
65+
{
66+
CaptureLog(SentryLogLevel.Info, template, parameters, null);
67+
}
68+
69+
/// <summary>
70+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Info"/>, when enabled and sampled.
71+
/// <para>This API is experimental and it may change in the future.</para>
72+
/// </summary>
73+
/// <param name="configureLog">A delegate to set attributes on the <see cref="SentryLog"/>. When the delegate throws an <see cref="Exception"/> during invocation, the log will not be captured.</param>
74+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
75+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
76+
[Experimental(DiagnosticId.ExperimentalFeature)]
77+
public void LogInfo(Action<SentryLog> configureLog, string template, params object[] parameters)
78+
{
79+
CaptureLog(SentryLogLevel.Info, template, parameters, configureLog);
80+
}
81+
82+
/// <summary>
83+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Warning"/>, when enabled and sampled.
84+
/// <para>This API is experimental and it may change in the future.</para>
85+
/// </summary>
86+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
87+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
88+
[Experimental(DiagnosticId.ExperimentalFeature)]
89+
public void LogWarning(string template, params object[] parameters)
90+
{
91+
CaptureLog(SentryLogLevel.Warning, template, parameters, null);
92+
}
93+
94+
/// <summary>
95+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Warning"/>, when enabled and sampled.
96+
/// <para>This API is experimental and it may change in the future.</para>
97+
/// </summary>
98+
/// <param name="configureLog">A delegate to set attributes on the <see cref="SentryLog"/>. When the delegate throws an <see cref="Exception"/> during invocation, the log will not be captured.</param>
99+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
100+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
101+
[Experimental(DiagnosticId.ExperimentalFeature)]
102+
public void LogWarning(Action<SentryLog> configureLog, string template, params object[] parameters)
103+
{
104+
CaptureLog(SentryLogLevel.Warning, template, parameters, configureLog);
105+
}
106+
107+
/// <summary>
108+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Error"/>, when enabled and sampled.
109+
/// <para>This API is experimental and it may change in the future.</para>
110+
/// </summary>
111+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
112+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
113+
[Experimental(DiagnosticId.ExperimentalFeature)]
114+
public void LogError(string template, params object[] parameters)
115+
{
116+
CaptureLog(SentryLogLevel.Error, template, parameters, null);
117+
}
118+
119+
/// <summary>
120+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Error"/>, when enabled and sampled.
121+
/// <para>This API is experimental and it may change in the future.</para>
122+
/// </summary>
123+
/// <param name="configureLog">A delegate to set attributes on the <see cref="SentryLog"/>. When the delegate throws an <see cref="Exception"/> during invocation, the log will not be captured.</param>
124+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
125+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
126+
[Experimental(DiagnosticId.ExperimentalFeature)]
127+
public void LogError(Action<SentryLog> configureLog, string template, params object[] parameters)
128+
{
129+
CaptureLog(SentryLogLevel.Error, template, parameters, configureLog);
130+
}
131+
132+
/// <summary>
133+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Fatal"/>, when enabled and sampled.
134+
/// <para>This API is experimental and it may change in the future.</para>
135+
/// </summary>
136+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
137+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
138+
[Experimental(DiagnosticId.ExperimentalFeature)]
139+
public void LogFatal(string template, params object[] parameters)
140+
{
141+
CaptureLog(SentryLogLevel.Fatal, template, parameters, null);
142+
}
143+
144+
/// <summary>
145+
/// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Fatal"/>, when enabled and sampled.
146+
/// <para>This API is experimental and it may change in the future.</para>
147+
/// </summary>
148+
/// <param name="configureLog">A delegate to set attributes on the <see cref="SentryLog"/>. When the delegate throws an <see cref="Exception"/> during invocation, the log will not be captured.</param>
149+
/// <param name="template">A formattable <see langword="string"/>. When incompatible with the <paramref name="parameters"/>, the log will not be captured. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
150+
/// <param name="parameters">The arguments to the <paramref name="template"/>. See <see href="https://learn.microsoft.com/dotnet/api/system.string.format">System.String.Format</see>.</param>
151+
[Experimental(DiagnosticId.ExperimentalFeature)]
152+
public void LogFatal(Action<SentryLog> configureLog, string template, params object[] parameters)
153+
{
154+
CaptureLog(SentryLogLevel.Fatal, template, parameters, configureLog);
155+
}
156+
}

0 commit comments

Comments
 (0)