|
1 | | -using Microsoft.AspNetCore; |
2 | 1 | using Serilog; |
3 | 2 | using Serilog.Events; |
4 | 3 |
|
5 | 4 | namespace Sentry.Samples.AspNetCore.Serilog; |
6 | 5 |
|
7 | 6 | public class Program |
8 | 7 | { |
9 | | - public static void Main(string[] args) => BuildWebHost(args).Run(); |
| 8 | + public static void Main(string[] args) => BuildWebApp(args).Run(); |
10 | 9 |
|
11 | | - public static IWebHost BuildWebHost(string[] args) => |
12 | | - WebHost.CreateDefaultBuilder(args) |
13 | | - |
14 | | - .UseSerilog((_, c) => |
15 | | - c.Enrich.FromLogContext() |
16 | | - .MinimumLevel.Debug() |
17 | | - .WriteTo.Console() |
18 | | - // Add Sentry integration with Serilog |
19 | | - // Two levels are used to configure it. |
20 | | - // One sets which log level is minimally required to keep a log message as breadcrumbs |
21 | | - // The other sets the minimum level for messages to be sent out as events to Sentry |
22 | | - .WriteTo.Sentry(s => |
23 | | - { |
24 | | - s.MinimumBreadcrumbLevel = LogEventLevel.Debug; |
25 | | - s.MinimumEventLevel = LogEventLevel.Error; |
26 | | - })) |
| 10 | + public static WebApplication BuildWebApp(string[] args) |
| 11 | + { |
| 12 | + var builder = WebApplication.CreateBuilder(args); |
| 13 | + builder.Host.UseSerilog((_, c) => |
| 14 | + c.Enrich.FromLogContext() |
| 15 | + .MinimumLevel.Debug() |
| 16 | + .WriteTo.Console() |
| 17 | + // Add Sentry integration with Serilog |
| 18 | + // Two levels are used to configure it. |
| 19 | + // One sets which log level is minimally required to keep a log message as breadcrumbs |
| 20 | + // The other sets the minimum level for messages to be sent out as events to Sentry |
| 21 | + .WriteTo.Sentry(s => |
| 22 | + { |
| 23 | + s.MinimumBreadcrumbLevel = LogEventLevel.Debug; |
| 24 | + s.MinimumEventLevel = LogEventLevel.Error; |
| 25 | + })); |
27 | 26 |
|
28 | | - // Add Sentry integration |
29 | | - // It can also be defined via configuration (including appsettings.json) |
30 | | - // or coded explicitly, via parameter like: |
31 | | - // .UseSentry("dsn") or .UseSentry(o => o.Dsn = ""; o.Release = "1.0"; ...) |
32 | | - .UseSentry() |
| 27 | + // Add Sentry integration |
| 28 | + // It can also be defined via configuration (including appsettings.json) |
| 29 | + // or coded explicitly, via parameter like: |
| 30 | + // .UseSentry("dsn") or .UseSentry(o => o.Dsn = ""; o.Release = "1.0"; ...) |
| 31 | + builder.WebHost.UseSentry(); |
33 | 32 |
|
34 | | - // The App: |
35 | | - .Configure(a => |
| 33 | + // The App: |
| 34 | + builder.WebHost.Configure(a => |
| 35 | + { |
| 36 | + // An example ASP.NET Core middleware that throws an |
| 37 | + // exception when serving a request to path: /throw |
| 38 | + a.Use(async (context, next) => |
36 | 39 | { |
37 | | - // An example ASP.NET Core middleware that throws an |
38 | | - // exception when serving a request to path: /throw |
39 | | - a.Use(async (context, next) => |
40 | | - { |
41 | | - // See MinimumBreadcrumbLevel set at the Serilog configuration above |
42 | | - Log.Logger.Debug("Static Serilog logger debug log stored as breadcrumbs."); |
| 40 | + // See MinimumBreadcrumbLevel set at the Serilog configuration above |
| 41 | + Log.Logger.Debug("Static Serilog logger debug log stored as breadcrumbs."); |
43 | 42 |
|
44 | | - var log = context.RequestServices.GetService<ILoggerFactory>() |
45 | | - .CreateLogger<Program>(); |
| 43 | + var log = context.RequestServices.GetRequiredService<ILoggerFactory>() |
| 44 | + .CreateLogger<Program>(); |
46 | 45 |
|
47 | | - log.LogInformation("Handling some request..."); |
| 46 | + log.LogInformation("Handling some request..."); |
48 | 47 |
|
49 | | - // Sends an event which includes the info and debug messages above |
50 | | - Log.Logger.Error("Logging using static Serilog directly also goes to Sentry."); |
| 48 | + // Sends an event which includes the info and debug messages above |
| 49 | + Log.Logger.Error("Logging using static Serilog directly also goes to Sentry."); |
51 | 50 |
|
52 | | - if (context.Request.Path == "/throw") |
| 51 | + if (context.Request.Path == "/throw") |
| 52 | + { |
| 53 | + var hub = context.RequestServices.GetRequiredService<IHub>(); |
| 54 | + hub.ConfigureScope(s => |
53 | 55 | { |
54 | | - var hub = context.RequestServices.GetService<IHub>(); |
55 | | - hub.ConfigureScope(s => |
56 | | - { |
57 | | - // More data can be added to the scope like this: |
58 | | - s.SetTag("Sample", "ASP.NET Core"); // indexed by Sentry |
59 | | - s.SetExtra("Extra!", "Some extra information"); |
60 | | - }); |
| 56 | + // More data can be added to the scope like this: |
| 57 | + s.SetTag("Sample", "ASP.NET Core"); // indexed by Sentry |
| 58 | + s.SetExtra("Extra!", "Some extra information"); |
| 59 | + }); |
| 60 | + |
| 61 | + // Logging through the ASP.NET Core `ILogger` while using Serilog |
| 62 | + log.LogInformation("Logging info..."); |
| 63 | + log.LogWarning("Logging some warning!"); |
61 | 64 |
|
62 | | - // Logging through the ASP.NET Core `ILogger` while using Serilog |
63 | | - log.LogInformation("Logging info..."); |
64 | | - log.LogWarning("Logging some warning!"); |
| 65 | + // The following exception will be captured by the SDK and the event |
| 66 | + // will include the Log messages and any custom scope modifications |
| 67 | + // as exemplified above. |
| 68 | + throw new Exception("An exception thrown from the ASP.NET Core pipeline"); |
| 69 | + } |
65 | 70 |
|
66 | | - // The following exception will be captured by the SDK and the event |
67 | | - // will include the Log messages and any custom scope modifications |
68 | | - // as exemplified above. |
69 | | - throw new Exception("An exception thrown from the ASP.NET Core pipeline"); |
70 | | - } |
| 71 | + await next(); |
| 72 | + }); |
| 73 | + }); |
71 | 74 |
|
72 | | - await next(); |
73 | | - }); |
74 | | - }) |
75 | | - .Build(); |
| 75 | + return builder.Build(); |
| 76 | + } |
76 | 77 | } |
0 commit comments