-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathProgram.cs
More file actions
76 lines (68 loc) · 4.3 KB
/
Program.cs
File metadata and controls
76 lines (68 loc) · 4.3 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
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
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Exceptionless.SampleHosting {
public class Program {
public static void Main(string[] args) {
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(builder => {
// By default sends warning and error log messages to Exceptionless.
// Log levels can be controlled remotely per log source from the Exceptionless app in near real-time.
builder.AddExceptionless();
})
.UseExceptionless() // initializes the client and flushes the queue during host shutdown
.ConfigureServices(services => {
// Reads settings from IConfiguration then adds additional configuration from this lambda.
// This also configures ExceptionlessClient.Default
services.AddExceptionless(c => c.DefaultData["Startup"] = "heyyy");
// OR
// services.AddExceptionless();
// OR
// services.AddExceptionless("API_KEY_HERE");
// adds a hosted service that will send sample events to Exceptionless.
services.AddHostedService<SampleService>();
})
.UseConsoleLifetime()
.ConfigureWebHostDefaults(builder => {
builder.Configure(app => {
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapGet("/ping", context => {
var client = context.RequestServices.GetRequiredService<ExceptionlessClient>();
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
// Submit a feature usage event directly using the client instance.
client.SubmitFeatureUsage("MapGet_Ping");
// This log message will get sent to Exceptionless since Exceptionless has be added to the logging system in Program.cs.
logger.LogWarning("Test warning message from ping");
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(client).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();
}
// This simulates an unhandled exception. Host-level Exceptionless integration reports
// host/AppDomain-level unhandled exceptions; ASP.NET Core request-pipeline exceptions
// require the ASP.NET Core integration and UseExceptionHandler.
throw new Exception($"Unhandled Exception: {Guid.NewGuid()}");
});
});
});
});
}
}