-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (60 loc) · 2.21 KB
/
Program.cs
File metadata and controls
74 lines (60 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseSentry(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
// Enable Sentry performance monitoring
options.TracesSampleRate = 1.0;
#if DEBUG
// Log debug information about the Sentry SDK
options.Debug = true;
#endif
// This option enables Logs sent to Sentry.
options.Experimental.EnableLogs = true;
});
var app = builder.Build();
// An example ASP.NET Core endpoint that throws an exception when serving a request to path: /throw
app.MapGet("/throw/{message?}", context =>
{
var exceptionMessage = context.GetRouteValue("message") as string;
var log = context.RequestServices.GetRequiredService<ILoggerFactory>()
.CreateLogger<Program>();
log.LogInformation("Handling some request...");
var hub = context.RequestServices.GetRequiredService<IHub>();
hub.ConfigureScope(s =>
{
// More data can be added to the scope like this:
s.SetTag("Sample", "ASP.NET Core"); // indexed by Sentry
s.SetExtra("Extra!", "Some extra information");
});
log.LogInformation("Logging info...");
log.LogWarning("Logging some warning!");
// The following exception will be captured by the SDK and the event
// will include the Log messages and any custom scope modifications
// as exemplified above.
throw new Exception(
exceptionMessage ?? "An exception thrown from the ASP.NET Core pipeline");
});
// Demonstrates how to add tracing in custom middleware
app.Use(async (context, next) =>
{
var span = SentrySdk.StartSpan("CustomMiddlewareSpan", "middleware");
try
{
var log = context.RequestServices.GetRequiredService<ILoggerFactory>()
.CreateLogger<Program>();
log.LogInformation("Just chilling for a bit...");
await Task.Delay(TimeSpan.FromMilliseconds(50)); // Simulate some work
span.Finish();
}
catch (Exception e)
{
span.Finish(e);
throw;
}
await next();
});
app.Run();