-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathHomeController.cs
85 lines (72 loc) · 2.88 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
using System.Diagnostics;
using aspnetcore8_mvc.Models;
using Microsoft.AspNetCore.Mvc;
namespace aspnetcore8_mvc.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
// The Bugsnag client (initialized in Program.cs) will be injected into your classes where you declare the IClient dependency.
// This allows you to report handled exceptions within your controller.
private readonly Bugsnag.IClient _bugsnag;
public HomeController(ILogger<HomeController> logger, Bugsnag.IClient bugsnag)
{
_logger = logger;
_bugsnag = bugsnag;
var request = HttpContext?.Request;
// A BeforeNotify callback lets you evaluate, modify, add and remove data before sending the error to bugsnag. The actions here will be applied to *all* errors, handled and unhandled.
_bugsnag.BeforeNotify(report =>
{
// In order to correlate errors with customer reports, or to see a list of users who experienced each error, you can attach user data in your callback
report.Event.User = new Bugsnag.Payload.User
{
Id = "006",
Name = "Hedy Lamarr",
Email = "h.lamarr@delos.com"
};
// This example makes some modifications that only apply to reports of error class "System.NotImplementedException".
if (report.OriginalException is NotImplementedException)
{
report.Event.Context = "an-important-context";
}
// note that calling report.Ignore() will discard the error report.
});
}
public IActionResult Index()
{
return View();
}
public IActionResult Contact()
{
// Report a handled exception
try
{
throw new Exception("Handled exception");
}
catch (Exception ex)
{
_bugsnag.Notify(ex, report =>
{
// You can also customise individual error reports before sending to Bugsnag.
report.Event.Metadata.Add("Don't worry", "I handled it");
});
}
return View();
}
public IActionResult Problems()
{
// You can leave manual breadcrumbs via the Breadcrumbs property on the client object
_bugsnag.Breadcrumbs.Leave("Here comes the exception...");
// You can optionally attach a type and metadata to a breadcrumb.
var metadata = new Dictionary<string, string> { { "message", "wait for it......" } };
_bugsnag.Breadcrumbs.Leave("Here comes the exception...", Bugsnag.BreadcrumbType.Navigation, metadata);
// Unhandled exceptions will be reported automatically
throw new NotImplementedException("We have a problem");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}