-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Program.cs
94 lines (75 loc) · 3.9 KB
/
Program.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
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
94
using Microsoft.Extensions.Logging;
using Sentry.Extensions.Logging;
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
builder.AddSentry(options =>
{
options.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
// Set to true to SDK debugging to see the internal messages through the logging library.
options.Debug = false;
// Configure the level of Sentry internal logging
options.DiagnosticLevel = SentryLevel.Debug;
options.MaxBreadcrumbs = 150; // Increasing from default 100
options.Release = "e386dfd"; // If not set here, SDK looks for it on main assembly's AssemblyInformationalVersion and AssemblyVersion
// Optionally configure options: The default values are:
options.MinimumBreadcrumbLevel = LogLevel.Information; // It requires at least this level to store breadcrumb
options.MinimumEventLevel = LogLevel.Error; // This level or above will result in event sent to Sentry
// Don't keep as a breadcrumb or send events for messages of level less than Critical with exception of type DivideByZeroException
options.AddLogEntryFilter((_, level, _, exception) => level < LogLevel.Critical && exception is DivideByZeroException);
options.ConfigureScope(s => s.SetTag("RootScope", "sent with all events"));
});
});
var logger = loggerFactory.CreateLogger<Program>();
logger.LogTrace("1 - By *default* this log level is ignored by Sentry.");
logger.LogInformation("2 - Information messages are stored as Breadcrumb, sent with the next event.");
// Won't add breadcrumb or record event due to the filter added above
logger.LogError(new DivideByZeroException(), "Ignored because of the LogEntryFilter added via options.");
// Log messages with variables are grouped together.
// This way a log message like: 'User {userId} logged in' doesn't generate 1 issue in Sentry for each user you have.
// When visualizing this issue in Sentry, you can press Next and Back to see the individual log entries:
logger.LogError("3 - This generates an event {id}, captured by sentry and includes breadcrumbs (2) tracked in this transaction.",
100);
logger.LogError("3 - This generates an event {id}, captured by sentry and includes breadcrumbs (2) tracked in this transaction.",
999);
using (logger.BeginScope(new Dictionary<string, string>
{
{ "A", "some value" },
{ "B", "more value" },
}))
{
logger.LogWarning("4 - Breadcrumb that only exists inside this scope");
logger.LogError("5 - An event that includes the scoped key-value (A, B) above and also the breadcrumbs: (2, 4) and event (3)");
using (logger.BeginScope("C - Inner most scope, with single string state"))
{
logger.LogInformation("6 - Inner most breadcrumb");
try
{
Dependency.Work("some work");
}
catch (Exception e)
{
// Handle an exception and log it:
logger.LogError(e, "7 - An event that includes the scope key-value (A, B, C) and also the breadcrumbs: (2, 4, 6) and events (3, 5)");
}
} // Dispose scope C, drops state C and breadcrumb 6
// An exception that will go unhandled and crash the app:
// Even though it's not caught nor logged, this error is captured by Sentry!
// It will include all the scope data available up to this point
Dependency.Work("8 - This unhandled exception is captured and includes Scope (A, B) and crumbs: (2, 4, 5) and event (3) ");
}
// Disposing the LoggerFactory will close the SDK since it was initialized through
// the integration while calling .Init()
internal static class Dependency
{
private static int Counter;
public static void Work(string message)
{
if (Counter == 10)
{
throw new InvalidOperationException(message);
}
Counter++;
Work(message);
}
}