-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
HomeController.cs
106 lines (91 loc) · 3.1 KB
/
HomeController.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
95
96
97
98
99
100
101
102
103
104
105
106
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Samples.AspNetCore.Mvc.Models;
namespace Samples.AspNetCore.Mvc.Controllers;
public class HomeController : Controller
{
private readonly IGameService _gameService;
private readonly ILogger<HomeController> _logger;
public HomeController(IGameService gameService, ILogger<HomeController> logger)
{
_gameService = gameService;
_logger = logger;
}
[HttpGet]
public IActionResult Index()
{
return View();
}
// Example: An exception that goes unhandled by the app will be captured by Sentry:
[HttpPost]
public async Task PostIndex(string @params)
{
try
{
if (@params == null)
{
_logger.LogWarning("Param {param} is null!", @params);
}
await _gameService.FetchNextPhaseDataAsync();
}
catch (Exception e)
{
var ioe = new InvalidOperationException("Bad POST! See Inner exception for details.", e);
ioe.Data.Add("inventory",
// The following anonymous object gets serialized:
new
{
SmallPotion = 3,
BigPotion = 0,
CheeseWheels = 512
});
throw ioe;
}
}
// Example: The view rendering throws: see about.cshtml
public IActionResult About(string who = null)
{
if (who == null)
{
// Exemplifies using the logger to raise a warning which will be sent as an event because MinimumEventLevel was configured to Warning
// ALso, the stack trace of this location will be sent (even though there was no exception) because of the configuration AttachStackTrace
_logger.LogWarning("A {route} '{value}' was requested.",
// example structured logging where keys (in the template above) go as tag keys and values below:
"/about",
"null");
}
return View();
}
// Example: To take the Sentry Hub and submit errors directly:
public IActionResult Contact(
// Hub holds a Client and Scope management
// Errors sent with the hub will include all context collected in the current scope
[FromServices] IHub sentry)
{
try
{
// Some code block that could throw
throw null;
}
catch (Exception e)
{
e.Data.Add("detail",
new
{
Reason = "There's a 'throw null' hard-coded here!",
IsCrazy = true
});
var id = sentry.CaptureException(e);
ViewData["Message"] = "An exception was caught and sent to Sentry! Event ID: " + id;
}
return View();
}
public IActionResult Test()
{
throw new Exception("Test exception thrown in controller!");
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}