Skip to content

Commit 2efa09d

Browse files
authored
Merge pull request #33 from getsentry/feat/capture-message
Capture message
2 parents 35a27b8 + 2ddab3b commit 2efa09d

File tree

5 files changed

+134
-6
lines changed

5 files changed

+134
-6
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using Sentry;
4+
using Sentry.Protocol;
45
using Sentry.Samples.Console.Customized;
56

67
// One of the ways to set your DSN is via an attribute:
@@ -65,13 +66,14 @@ await SentrySdk.ConfigureScopeAsync(async scope =>
6566
scope.SetExtra("Key", "Value");
6667
});
6768

68-
SentrySdk.CaptureException(new Exception("Something went wrong."));
69+
SentrySdk.CaptureMessage("Some warning!", SentryLevel.Warning);
6970

7071
// -------------------------
7172

7273
// A custom made client, that could be registered with DI,
7374
// would get disposed by the container on app shutdown
7475

76+
SentrySdk.CaptureMessage("Starting new client");
7577
// Using a different DSN:
7678
var adminDsn = new Dsn(AdminDsn);
7779
using (var adminClient = new SentryClient(new SentryOptions { Dsn = adminDsn }))

src/Sentry/SentryClientExtensions.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.ComponentModel;
3+
using Sentry.Protocol;
34

45
namespace Sentry
56
{
@@ -15,12 +16,34 @@ public static class SentryClientExtensions
1516
/// <param name="client">The Sentry client.</param>
1617
/// <param name="ex">The exception.</param>
1718
/// <param name="isUnhandled">Whether the error was not handled by the application</param>
18-
/// <returns></returns>
19+
/// <returns>The Id of the event</returns>
1920
public static Guid CaptureException(this ISentryClient client, Exception ex, bool? isUnhandled = null)
2021
{
2122
return !client.IsEnabled
2223
? Guid.Empty
2324
: client.CaptureEvent(new SentryEvent(ex, isUnhandled: isUnhandled));
2425
}
26+
27+
/// <summary>
28+
/// Captures a message.
29+
/// </summary>
30+
/// <param name="client">The Sentry client.</param>
31+
/// <param name="message">The message to send.</param>
32+
/// <param name="level">The message level.</param>
33+
/// <returns>The Id of the event</returns>
34+
public static Guid CaptureMessage(
35+
this ISentryClient client,
36+
string message,
37+
SentryLevel level = SentryLevel.Info)
38+
{
39+
return !client.IsEnabled
40+
|| string.IsNullOrWhiteSpace(message)
41+
? Guid.Empty
42+
: client.CaptureEvent(new SentryEvent
43+
{
44+
Message = message,
45+
Level = level
46+
});
47+
}
2548
}
2649
}

src/Sentry/SentrySdk.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public static void ConfigureScope(Action<Scope> configureScope)
223223
/// Configures the scope asynchronously
224224
/// </summary>
225225
/// <param name="configureScope">The configure scope callback.</param>
226-
/// <returns></returns>
226+
/// <returns>The Id of the event</returns>
227227
[DebuggerStepThrough]
228228
public static Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
229229
=> _hub.ConfigureScopeAsync(configureScope);
@@ -232,7 +232,7 @@ public static Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
232232
/// Captures the event.
233233
/// </summary>
234234
/// <param name="evt">The event.</param>
235-
/// <returns></returns>
235+
/// <returns>The Id of the event</returns>
236236
[DebuggerStepThrough]
237237
public static Guid CaptureEvent(SentryEvent evt)
238238
=> _hub.CaptureEvent(evt);
@@ -242,7 +242,7 @@ public static Guid CaptureEvent(SentryEvent evt)
242242
/// </summary>
243243
/// <param name="evt">The event.</param>
244244
/// <param name="scope">The scope.</param>
245-
/// <returns></returns>
245+
/// <returns>The Id of the event</returns>
246246
[DebuggerStepThrough]
247247
[EditorBrowsable(EditorBrowsableState.Never)]
248248
public static Guid CaptureEvent(SentryEvent evt, Scope scope)
@@ -252,7 +252,7 @@ public static Guid CaptureEvent(SentryEvent evt, Scope scope)
252252
/// Captures the exception.
253253
/// </summary>
254254
/// <param name="exception">The exception.</param>
255-
/// <returns></returns>
255+
/// <returns>The Id of the event</returns>
256256
[DebuggerStepThrough]
257257
public static Guid CaptureException(Exception exception)
258258
=> _hub.CaptureException(exception);
@@ -271,5 +271,15 @@ public static Guid CaptureException(Exception exception)
271271
[EditorBrowsable(EditorBrowsableState.Never)]
272272
public static Guid CaptureException(Exception exception, bool? isUnhandled)
273273
=> _hub.CaptureException(exception, isUnhandled);
274+
275+
/// <summary>
276+
/// Captures the message.
277+
/// </summary>
278+
/// <param name="message">The message to send.</param>
279+
/// <param name="level">The message level.</param>
280+
/// <returns>The Id of the event</returns>
281+
[DebuggerStepThrough]
282+
public static Guid CaptureMessage(string message, SentryLevel level = SentryLevel.Info)
283+
=> _hub.CaptureMessage(message, level);
274284
}
275285
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using NSubstitute;
3+
using Sentry.Protocol;
4+
using Xunit;
5+
6+
namespace Sentry.Tests
7+
{
8+
public class SentryClientExtensionsTests
9+
{
10+
private readonly ISentryClient _sut = Substitute.For<ISentryClient>();
11+
12+
[Fact]
13+
public void CaptureException_DisabledClient_DoesNotCaptureEvent()
14+
{
15+
_sut.IsEnabled.Returns(false);
16+
var id = _sut.CaptureException(new Exception());
17+
18+
_sut.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());
19+
Assert.Equal(default, id);
20+
}
21+
22+
[Fact]
23+
public void CaptureException_EnabledClient_CapturesEvent()
24+
{
25+
_sut.IsEnabled.Returns(true);
26+
_sut.CaptureException(new Exception());
27+
_sut.Received(1).CaptureEvent(Arg.Any<SentryEvent>());
28+
}
29+
30+
[Fact]
31+
public void CaptureMessage_DisabledClient_DoesNotCaptureEvent()
32+
{
33+
_sut.IsEnabled.Returns(false);
34+
var id = _sut.CaptureMessage("Message");
35+
36+
_sut.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());
37+
Assert.Equal(default, id);
38+
}
39+
40+
[Fact]
41+
public void CaptureMessage_EnabledClient_CapturesEvent()
42+
{
43+
_sut.IsEnabled.Returns(true);
44+
_sut.CaptureMessage("Message");
45+
_sut.Received(1).CaptureEvent(Arg.Any<SentryEvent>());
46+
}
47+
48+
[Fact]
49+
public void CaptureMessage_Level_CapturesEventWithLevel()
50+
{
51+
const SentryLevel expectedLevel = SentryLevel.Fatal;
52+
_sut.IsEnabled.Returns(true);
53+
_sut.CaptureMessage("Message", expectedLevel);
54+
_sut.Received(1).CaptureEvent(Arg.Is<SentryEvent>(e => e.Level == expectedLevel));
55+
}
56+
57+
[Fact]
58+
public void CaptureMessage_Message_CapturesEventWithMessage()
59+
{
60+
const string expectedMessage = "message";
61+
_sut.IsEnabled.Returns(true);
62+
_sut.CaptureMessage(expectedMessage);
63+
_sut.Received(1).CaptureEvent(Arg.Is<SentryEvent>(e => e.Message == expectedMessage));
64+
}
65+
66+
[Fact]
67+
public void CaptureMessage_WhitespaceMessage_DoesNotCapturesEventWithMessage()
68+
{
69+
_sut.IsEnabled.Returns(true);
70+
var id = _sut.CaptureMessage(" ");
71+
72+
_sut.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());
73+
Assert.Equal(default, id);
74+
}
75+
76+
[Fact]
77+
public void CaptureMessage_NullMessage_DoesNotCapturesEventWithMessage()
78+
{
79+
_sut.IsEnabled.Returns(true);
80+
var id = _sut.CaptureMessage(null);
81+
82+
_sut.DidNotReceive().CaptureEvent(Arg.Any<SentryEvent>());
83+
Assert.Equal(default, id);
84+
}
85+
}
86+
}

test/Sentry.Tests/SentrySdkTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Reflection;
44
using System.Threading.Tasks;
55
using Sentry.Extensibility;
6+
using Sentry.Protocol;
67
using Sentry.Testing;
78
using Sentry.Tests.Helpers;
89
using Xunit;
@@ -240,6 +241,12 @@ await SentrySdk.ConfigureScopeAsync(_ =>
240241
[Fact]
241242
public void CaptureException_InstanceUnhandled_NoOp() => SentrySdk.CaptureException(new Exception(), true);
242243

244+
[Fact]
245+
public void CaptureMessage_Message_NoOp() => SentrySdk.CaptureMessage("message");
246+
247+
[Fact]
248+
public void CaptureMessage_MessageLevel_NoOp() => SentrySdk.CaptureMessage("message", SentryLevel.Debug);
249+
243250
[Fact]
244251
public void Implements_Client()
245252
{

0 commit comments

Comments
 (0)