-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (77 loc) · 2.91 KB
/
Program.cs
File metadata and controls
93 lines (77 loc) · 2.91 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.ComponentModel.DataAnnotations;
using System.Data.Common;
using System.Data.Entity;
using var _ = SentrySdk.Init(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
options.Debug = true; // To see SDK logs on the console
options.TracesSampleRate = 1.0;
// Add the EntityFramework integration to the SentryOptions of your app startup code:
options.AddEntityFramework();
});
var dbConnection = Effort.DbConnectionFactory.CreateTransient();
dbConnection.SetConnectionTimeout(60);
var db = new SampleDbContext(dbConnection, true);
// This creates a transaction where each insertion will be registered into the active transaction.
Console.WriteLine("Some Http Post request");
SentrySdk.ConfigureScope(scope =>
{
scope.Transaction = SentrySdk.StartTransaction("/Start", "Create");
var manualSpan = scope.Transaction.StartChild("Database Fill");
//Populate the database
for (var j = 0; j < 10; j++)
{
db.Users.Add(new SampleUser { Id = j, RequiredColumn = "123" });
}
db.Users.Add(new SampleUser { Id = 52, RequiredColumn = "Bill" });
manualSpan.Finish();
manualSpan = scope.Transaction.StartChild("Save changes");
db.SaveChanges();
manualSpan.Finish();
scope.Transaction.Finish();
});
// This simulates a search operation, creating a new transaction for the requested data.
SentrySdk.ConfigureScope(scope =>
{
Console.WriteLine("Searching for users named Bill");
scope.Transaction = SentrySdk.StartTransaction("/Users?name=Bill", "GET");
var manualSpan = scope.Transaction.StartChild("manual - search");
var query = db.Users
.Where(s => s.RequiredColumn == "Bill")
.ToList();
manualSpan.Finish();
scope.Transaction.Finish();
Console.WriteLine($"Found {query.Count} users.");
});
// This simulates a search operation, creating a new transaction for the requested data.
SentrySdk.ConfigureScope(scope =>
{
Console.WriteLine("Searching for users with Id higher than 5...");
scope.Transaction = SentrySdk.StartTransaction("/Users?id>5", "GET");
var query2 = db.Users.Where(user => user.Id > 5).ToList();
scope.Transaction.Finish();
Console.WriteLine($"Found {query2.Count} users.");
});
// This will throw a DbEntityValidationException and crash the app
// But Sentry will capture the error.
var user = new SampleUser();
db.Users.Add(user);
db.SaveChanges();
public class SampleUser
{
[Key]
public int Id { get; set; }
[Required]
public string RequiredColumn { get; set; }
}
public class SampleDbContext : DbContext
{
public DbSet<SampleUser> Users { get; set; }
public SampleDbContext(DbConnection connection, bool ownsConnection)
: base(connection, ownsConnection)
{ }
}