-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathValuesController.cs
More file actions
50 lines (43 loc) · 2.44 KB
/
ValuesController.cs
File metadata and controls
50 lines (43 loc) · 2.44 KB
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
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Exceptionless.SampleAspNetCore.Controllers {
[Route("api/[controller]")]
public class ValuesController : Controller {
private readonly ExceptionlessClient _exceptionlessClient;
private readonly ILogger _logger;
public ValuesController(ExceptionlessClient exceptionlessClient, ILogger<ValuesController> logger) {
// ExceptionlessClient instance from DI that was registered with the builder.AddExceptionless call in Program.cs.
_exceptionlessClient = exceptionlessClient;
_logger = logger;
}
// GET api/values
[HttpGet]
public Dictionary<string, string> Get() {
// Submit a feature usage event directly using the client instance that is injected from the DI container.
_exceptionlessClient.SubmitFeatureUsage("ValuesController_Get");
// This log message will get sent to Exceptionless since Exceptionless has been added to the logging system in Program.cs.
_logger.LogWarning("Test warning message");
try {
throw new Exception($"Handled Exception: {Guid.NewGuid()}");
}
catch (Exception handledException) {
// Use the ToExceptionless extension method to submit this handled exception to Exceptionless using the client instance from DI.
handledException.ToExceptionless(_exceptionlessClient).Submit();
}
try {
throw new Exception($"Handled Exception (Default Client): {Guid.NewGuid()}");
}
catch (Exception handledException) {
// Use the ToExceptionless extension method to submit this handled exception to Exceptionless using the default client instance (ExceptionlessClient.Default).
// This works and is convenient, but its generally not recommended to use static singleton instances because it makes testing and
// other things harder.
handledException.ToExceptionless().Submit();
}
// Unhandled exceptions will get reported because Program.cs enables the built-in exception handler pipeline
// and wires Exceptionless into both ASP.NET Core diagnostics and middleware hooks.
throw new Exception($"Unhandled Exception: {Guid.NewGuid()}");
}
}
}