|
1 | 1 | using Microsoft.Extensions.Logging; |
2 | 2 | using Sentry.Extensions.Logging; |
3 | 3 |
|
4 | | -namespace Sentry.Samples.ME.Logging; |
5 | | - |
6 | | -internal class Program |
| 4 | +using var loggerFactory = LoggerFactory.Create(builder => |
| 5 | +{ |
| 6 | + builder.AddConsole(); |
| 7 | + builder.AddSentry(o => |
| 8 | + { |
| 9 | + // Set to true to SDK debugging to see the internal messages through the logging library. |
| 10 | + o.Debug = false; |
| 11 | + // Configure the level of Sentry internal logging |
| 12 | + o.DiagnosticLevel = SentryLevel.Debug; |
| 13 | + |
| 14 | + o.Dsn = "https://[email protected]/5428537"; |
| 15 | + o.MaxBreadcrumbs = 150; // Increasing from default 100 |
| 16 | + o.Release = "e386dfd"; // If not set here, SDK looks for it on main assembly's AssemblyInformationalVersion and AssemblyVersion |
| 17 | + |
| 18 | + // Optionally configure options: The default values are: |
| 19 | + o.MinimumBreadcrumbLevel = LogLevel.Information; // It requires at least this level to store breadcrumb |
| 20 | + o.MinimumEventLevel = LogLevel.Error; // This level or above will result in event sent to Sentry |
| 21 | + |
| 22 | + // Don't keep as a breadcrumb or send events for messages of level less than Critical with exception of type DivideByZeroException |
| 23 | + o.AddLogEntryFilter((_, level, _, exception) => level < LogLevel.Critical && exception is DivideByZeroException); |
| 24 | + |
| 25 | + o.ConfigureScope(s => s.SetTag("RootScope", "sent with all events")); |
| 26 | + }); |
| 27 | +}); |
| 28 | +var logger = loggerFactory.CreateLogger<Program>(); |
| 29 | + |
| 30 | +logger.LogTrace("1 - By *default* this log level is ignored by Sentry."); |
| 31 | + |
| 32 | +logger.LogInformation("2 - Information messages are stored as Breadcrumb, sent with the next event."); |
| 33 | + |
| 34 | +// Won't add breadcrumb or record event due to the filter added above |
| 35 | +logger.LogError(new DivideByZeroException(), "Ignored because of the LogEntryFilter added via options."); |
| 36 | + |
| 37 | +// Log messages with variables are grouped together. |
| 38 | +// This way a log message like: 'User {userId} logged in' doesn't generate 1 issue in Sentry for each user you have. |
| 39 | +// When visualizing this issue in Sentry, you can press Next and Back to see the individual log entries: |
| 40 | +logger.LogError("3 - This generates an event {id}, captured by sentry and includes breadcrumbs (2) tracked in this transaction.", |
| 41 | + 100); |
| 42 | +logger.LogError("3 - This generates an event {id}, captured by sentry and includes breadcrumbs (2) tracked in this transaction.", |
| 43 | + 999); |
| 44 | + |
| 45 | +using (logger.BeginScope(new Dictionary<string, string> |
| 46 | + { |
| 47 | + { "A", "some value" }, |
| 48 | + { "B", "more value" }, |
| 49 | + })) |
7 | 50 | { |
8 | | - private static void Main() |
| 51 | + logger.LogWarning("4 - Breadcrumb that only exists inside this scope"); |
| 52 | + |
| 53 | + logger.LogError("5 - An event that includes the scoped key-value (A, B) above and also the breadcrumbs: (2, 4) and event (3)"); |
| 54 | + |
| 55 | + using (logger.BeginScope("C - Inner most scope, with single string state")) |
9 | 56 | { |
10 | | - using var loggerFactory = LoggerFactory.Create(builder => |
| 57 | + logger.LogInformation("6 - Inner most breadcrumb"); |
| 58 | + |
| 59 | + try |
11 | 60 | { |
12 | | - builder.AddConsole(); |
13 | | - builder.AddSentry(o => |
14 | | - { |
15 | | - // Set to true to SDK debugging to see the internal messages through the logging library. |
16 | | - o.Debug = false; |
17 | | - // Configure the level of Sentry internal logging |
18 | | - o.DiagnosticLevel = SentryLevel.Debug; |
19 | | - |
20 | | - o.Dsn = "https://[email protected]/5428537"; |
21 | | - o.MaxBreadcrumbs = 150; // Increasing from default 100 |
22 | | - o.Release = "e386dfd"; // If not set here, SDK looks for it on main assembly's AssemblyInformationalVersion and AssemblyVersion |
23 | | - |
24 | | - // Optionally configure options: The default values are: |
25 | | - o.MinimumBreadcrumbLevel = LogLevel.Information; // It requires at least this level to store breadcrumb |
26 | | - o.MinimumEventLevel = LogLevel.Error; // This level or above will result in event sent to Sentry |
27 | | - |
28 | | - // Don't keep as a breadcrumb or send events for messages of level less than Critical with exception of type DivideByZeroException |
29 | | - o.AddLogEntryFilter((_, level, _, exception) => level < LogLevel.Critical && exception is DivideByZeroException); |
30 | | - |
31 | | - o.ConfigureScope(s => s.SetTag("RootScope", "sent with all events")); |
32 | | - }); |
33 | | - }); |
34 | | - var logger = loggerFactory.CreateLogger<Program>(); |
35 | | - |
36 | | - logger.LogTrace("1 - By *default* this log level is ignored by Sentry."); |
37 | | - |
38 | | - logger.LogInformation("2 - Information messages are stored as Breadcrumb, sent with the next event."); |
39 | | - |
40 | | - // Won't add breadcrumb or record event due to the filter added above |
41 | | - logger.LogError(new DivideByZeroException(), "Ignored because of the LogEntryFilter added via options."); |
42 | | - |
43 | | - // Log messages with variables are grouped together. |
44 | | - // This way a log message like: 'User {userId} logged in' doesn't generate 1 issue in Sentry for each user you have. |
45 | | - // When visualizing this issue in Sentry, you can press Next and Back to see the individual log entries: |
46 | | - logger.LogError("3 - This generates an event {id}, captured by sentry and includes breadcrumbs (2) tracked in this transaction.", |
47 | | - 100); |
48 | | - logger.LogError("3 - This generates an event {id}, captured by sentry and includes breadcrumbs (2) tracked in this transaction.", |
49 | | - 999); |
50 | | - |
51 | | - using (logger.BeginScope(new Dictionary<string, string> |
52 | | - { |
53 | | - {"A", "some value"}, |
54 | | - {"B", "more value"}, |
55 | | - })) |
| 61 | + Dependency.Work("some work"); |
| 62 | + } |
| 63 | + catch (Exception e) |
56 | 64 | { |
57 | | - logger.LogWarning("4 - Breadcrumb that only exists inside this scope"); |
58 | | - |
59 | | - logger.LogError("5 - An event that includes the scoped key-value (A, B) above and also the breadcrumbs: (2, 4) and event (3)"); |
60 | | - |
61 | | - using (logger.BeginScope("C - Inner most scope, with single string state")) |
62 | | - { |
63 | | - logger.LogInformation("6 - Inner most breadcrumb"); |
64 | | - |
65 | | - try |
66 | | - { |
67 | | - Dependency.Work("some work"); |
68 | | - } |
69 | | - catch (Exception e) |
70 | | - { |
71 | | - // Handle an exception and log it: |
72 | | - 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)"); |
73 | | - } |
74 | | - } // Dispose scope C, drops state C and breadcrumb 6 |
75 | | - |
76 | | - // An exception that will go unhandled and crash the app: |
77 | | - // Even though it's not caught nor logged, this error is captured by Sentry! |
78 | | - // It will include all the scope data available up to this point |
79 | | - Dependency.Work("8 - This unhandled exception is captured and includes Scope (A, B) and crumbs: (2, 4, 5) and event (3) "); |
| 65 | + // Handle an exception and log it: |
| 66 | + 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)"); |
80 | 67 | } |
| 68 | + } // Dispose scope C, drops state C and breadcrumb 6 |
81 | 69 |
|
82 | | - // Disposing the LoggerFactory will close the SDK since it was initialized through |
83 | | - // the integration while calling .Init() |
84 | | - } |
| 70 | + // An exception that will go unhandled and crash the app: |
| 71 | + // Even though it's not caught nor logged, this error is captured by Sentry! |
| 72 | + // It will include all the scope data available up to this point |
| 73 | + Dependency.Work("8 - This unhandled exception is captured and includes Scope (A, B) and crumbs: (2, 4, 5) and event (3) "); |
85 | 74 | } |
86 | 75 |
|
| 76 | +// Disposing the LoggerFactory will close the SDK since it was initialized through |
| 77 | +// the integration while calling .Init() |
| 78 | + |
87 | 79 | internal static class Dependency |
88 | 80 | { |
89 | 81 | private static int Counter; |
|
0 commit comments