-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStartup.cs
68 lines (54 loc) · 2.51 KB
/
Startup.cs
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
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using SentryDotNet.AspNetCore;
namespace SentryDotNet.AspNetCoreTestApp
{
public class Startup
{
private readonly IHostingEnvironment _env;
public Startup(IHostingEnvironment env)
{
_env = env;
}
public void ConfigureServices(IServiceCollection services)
{
// Add a DSN for test purposes here:
var dsn = "";
services.AddSentryDotNet(
new SentryClient(
dsn,
new SentryEventDefaults(
environment: _env.EnvironmentName,
release: typeof(Startup).Assembly.GetName().Version.ToString(3),
logger: _env.ApplicationName)));
services.AddAuthentication(o => { o.DefaultScheme = ApiKeyAuthenticationHandler.SchemeName; })
.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(
ApiKeyAuthenticationHandler.SchemeName,
"API key authentication",
o => { o.AllowedApiKeys = new[] { "someKey" }; });
}
public void Configure(IApplicationBuilder app)
{
// Make sure middleware that catches exceptions without rethrowing them is added *before* SentryDotNet
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseSentryDotNet(new SentryDotNetOptions { CaptureRequestBody = true });
app.Run(async context => { await DoSomethingAsync(context); });
}
private static async Task DoSomethingAsync(HttpContext context)
{
var eventBuilder = (SentryEventBuilder)context.Items[SentryDotNetMiddleware.EventBuilderKey];
eventBuilder.Breadcrumbs.Add(new SentryBreadcrumb("some.breadcrumb") { Message = "I am a breadcrumb" });
if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("error"))
{
throw new InvalidOperationException("Boom");
}
await context.Response.WriteAsync($"All good. You are '{context.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value}'.");
}
}
}