Skip to content

Commit fbce433

Browse files
authored
Adding support for Microsoft.Extensions.Hosting (generic host). Improving DI support throughout. (#241)
1 parent 9e91a51 commit fbce433

File tree

20 files changed

+373
-173
lines changed

20 files changed

+373
-173
lines changed

Exceptionless.Net.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exceptionless.SampleWindows
7474
EndProject
7575
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exceptionless.SampleWpf", "samples\Exceptionless.SampleWpf\Exceptionless.SampleWpf.csproj", "{AD95A70D-18DB-414E-9F6C-1B13B4FBA56E}"
7676
EndProject
77+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exceptionless.Extensions.Hosting", "src\Platforms\Exceptionless.Extensions.Hosting\Exceptionless.Extensions.Hosting.csproj", "{A5589072-EC59-4A6A-B78D-5D2ABB36DB1B}"
78+
EndProject
7779
Global
7880
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7981
Debug|Any CPU = Debug|Any CPU
@@ -168,6 +170,10 @@ Global
168170
{AD95A70D-18DB-414E-9F6C-1B13B4FBA56E}.Debug|Any CPU.Build.0 = Debug|Any CPU
169171
{AD95A70D-18DB-414E-9F6C-1B13B4FBA56E}.Release|Any CPU.ActiveCfg = Release|Any CPU
170172
{AD95A70D-18DB-414E-9F6C-1B13B4FBA56E}.Release|Any CPU.Build.0 = Release|Any CPU
173+
{A5589072-EC59-4A6A-B78D-5D2ABB36DB1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
174+
{A5589072-EC59-4A6A-B78D-5D2ABB36DB1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
175+
{A5589072-EC59-4A6A-B78D-5D2ABB36DB1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
176+
{A5589072-EC59-4A6A-B78D-5D2ABB36DB1B}.Release|Any CPU.Build.0 = Release|Any CPU
171177
EndGlobalSection
172178
GlobalSection(SolutionProperties) = preSolution
173179
HideSolutionNode = FALSE
@@ -194,6 +200,7 @@ Global
194200
{683D19D9-1AF2-4F4B-B0E6-24F26A369436} = {D363E15F-621D-40E4-8C96-DEE41A7070FF}
195201
{E1D077DE-C62E-4137-B609-3E9845C8A027} = {2CEE12C6-3840-4C01-A952-D3026B0A662A}
196202
{AD95A70D-18DB-414E-9F6C-1B13B4FBA56E} = {2CEE12C6-3840-4C01-A952-D3026B0A662A}
203+
{A5589072-EC59-4A6A-B78D-5D2ABB36DB1B} = {D363E15F-621D-40E4-8C96-DEE41A7070FF}
197204
EndGlobalSection
198205
GlobalSection(ExtensibilityGlobals) = postSolution
199206
SolutionGuid = {EBB2CC85-FF87-431B-865F-2F110B2A10E6}
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
using System;
22
using System.Collections.Generic;
3-
using Exceptionless.Logging;
43
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Logging;
55

66
namespace Exceptionless.SampleAspNetCore.Controllers {
77
[Route("api/[controller]")]
88
public class ValuesController : Controller {
9+
private readonly ExceptionlessClient _exceptionlessClient;
10+
private readonly ILogger _logger;
11+
12+
public ValuesController(ExceptionlessClient exceptionlessClient, ILogger<ValuesController> logger) {
13+
_exceptionlessClient = exceptionlessClient;
14+
_logger = logger;
15+
}
16+
917
// GET api/values
1018
[HttpGet]
1119
public Dictionary<string, string> Get() {
12-
ExceptionlessClient.Default.CreateLog("ValuesController", "Getting results", LogLevel.Info).Submit();
13-
throw new Exception($"Random AspNetCore Exception: {Guid.NewGuid()}");
20+
// Submit a feature usage event directly using the client instance that is injected from the DI container.
21+
_exceptionlessClient.SubmitFeatureUsage("ValuesController_Get");
22+
23+
// This log message will get sent to Exceptionless since Exceptionless has be added to the logging system in Program.cs.
24+
_logger.LogWarning("Test warning message");
25+
26+
try {
27+
throw new Exception($"Handled Exception: {Guid.NewGuid()}");
28+
} catch (Exception handledException) {
29+
// Use the ToExceptionless extension method to submit this handled exception to Exceptionless using the client instance from DI.
30+
handledException.ToExceptionless(_exceptionlessClient).Submit();
31+
}
32+
33+
// Unhandled exceptions will get reported since called UseExceptionless in the Startup.cs which registers a listener for unhandled exceptions.
34+
throw new Exception($"Unhandled Exception: {Guid.NewGuid()}");
1435
}
1536
}
1637
}

samples/Exceptionless.SampleAspNetCore/Exceptionless.SampleAspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<ItemGroup>
1414
<ProjectReference Include="..\..\src\Exceptionless\Exceptionless.csproj" />
1515
<ProjectReference Include="..\..\src\Platforms\Exceptionless.AspNetCore\Exceptionless.AspNetCore.csproj" />
16+
<ProjectReference Include="..\..\src\Platforms\Exceptionless.Extensions.Hosting\Exceptionless.Extensions.Hosting.csproj" />
1617
<ProjectReference Include="..\..\src\Platforms\Exceptionless.Extensions.Logging\Exceptionless.Extensions.Logging.csproj" />
1718
</ItemGroup>
1819
</Project>
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
using System;
2-
using Microsoft.AspNetCore;
32
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Hosting;
44

55
namespace Exceptionless.SampleAspNetCore {
66
public class Program {
77
public static void Main(string[] args) {
8-
CreateWebHostBuilder(args).Build().Run();
8+
CreateHostBuilder(args).Build().Run();
99
}
1010

11-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
12-
return WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
13-
}
11+
public static IHostBuilder CreateHostBuilder(string[] args) =>
12+
Host.CreateDefaultBuilder(args)
13+
.ConfigureLogging(b => {
14+
// By default sends warning and error log messages to Exceptionless.
15+
// Log levels can be controlled remotely per log source from the Exceptionless app in near real-time.
16+
b.AddExceptionless();
17+
})
18+
.ConfigureWebHostDefaults(webBuilder => {
19+
webBuilder.UseStartup<Startup>();
20+
});
1421
}
1522
}

samples/Exceptionless.SampleAspNetCore/Properties/launchSettings.json

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
{
2-
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
5-
"iisExpress": {
6-
"applicationUrl": "http://localhost:31084/",
7-
"sslPort": 0
8-
}
9-
},
102
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
13-
"launchBrowser": true,
14-
"launchUrl": "api/values",
15-
"environmentVariables": {
16-
"ASPNETCORE_ENVIRONMENT": "Development"
17-
}
18-
},
193
"Exceptionless.SampleAspNetCore": {
204
"commandName": "Project",
215
"launchBrowser": true,

samples/Exceptionless.SampleAspNetCore/Startup.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
using System;
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.AspNetCore.Hosting;
4-
using Microsoft.AspNetCore.Mvc;
54
using Microsoft.Extensions.Configuration;
65
using Microsoft.Extensions.DependencyInjection;
7-
using Microsoft.Extensions.Logging;
86

97
namespace Exceptionless.SampleAspNetCore {
108
public class Startup {
11-
public Startup(IWebHostEnvironment env) {
12-
var builder = new ConfigurationBuilder()
13-
.SetBasePath(env.ContentRootPath)
14-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
15-
.AddEnvironmentVariables();
16-
Configuration = builder.Build();
9+
public Startup(IConfiguration configuration) {
10+
Configuration = configuration;
1711
}
1812

19-
public IConfigurationRoot Configuration { get; }
13+
public IConfiguration Configuration { get; }
2014

2115
public void ConfigureServices(IServiceCollection services) {
22-
services.AddLogging(b => b
23-
.AddConfiguration(Configuration.GetSection("Logging"))
24-
.AddDebug()
25-
.AddConsole()
26-
.AddExceptionless());
16+
// Reads settings from IConfiguration
17+
services.AddExceptionless(c => c.DefaultData["Startup"] = "heyyy");
18+
// OR
19+
// services.AddExceptionless(c => c.ApiKey = "API_KEY_HERE");
20+
// OR
21+
// services.AddExceptionless("API_KEY_HERE");
22+
23+
// This enables Exceptionless to gather more detailed information about unhandled exceptions and other events
2724
services.AddHttpContextAccessor();
28-
services.AddMvc();
25+
services.AddControllers();
2926
}
3027

31-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) {
32-
app.UseExceptionless(Configuration);
33-
//OR
34-
//app.UseExceptionless(c => c.ReadFromConfiguration(Configuration));
35-
//OR
36-
//app.UseExceptionless("API_KEY_HERE");
37-
//OR
38-
//loggerFactory.AddExceptionless("API_KEY_HERE");
39-
//OR
40-
//loggerFactory.AddExceptionless(c => c.ReadFromConfiguration(Configuration));
28+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
29+
// Adds Exceptionless middleware to listen for unhandled exceptions
30+
app.UseExceptionless();
4131

42-
//loggerFactory.AddExceptionless();
4332
app.UseRouting();
4433
app.UseEndpoints(endpoints => {
4534
endpoints.MapControllers();

samples/Exceptionless.SampleAspNetCore/web.config

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Exceptionless/Extensions/ExceptionExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ public static EventBuilder ToExceptionless(this Exception exception, ContextData
2929
pluginContextData.SetException(exception);
3030
return client.CreateEvent(pluginContextData).SetType(Event.KnownTypes.Error);
3131
}
32+
33+
/// <summary>
34+
/// Creates a builder object for constructing error reports in a fluent api.
35+
/// </summary>
36+
/// <param name="exception">The exception.</param>
37+
/// <param name="client">
38+
/// The ExceptionlessClient instance used for configuration.
39+
/// </param>
40+
/// <returns></returns>
41+
public static EventBuilder ToExceptionless(this Exception exception, ExceptionlessClient client) {
42+
if (client == null)
43+
throw new ArgumentNullException(nameof(client));
44+
45+
var pluginContextData = new ContextData();
46+
pluginContextData.SetException(exception);
47+
48+
return client.CreateEvent(pluginContextData).SetType(Event.KnownTypes.Error);
49+
}
3250
}
3351
}
3452

src/Exceptionless/Models/Collections/SettingsDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public LogLevel GetMinLogLevel(string source) {
201201
if (_minLogLevels.TryGetValue(source, out minLogLevel))
202202
return minLogLevel;
203203

204-
string setting = GetTypeAndSourceSetting("log", source, "Trace");
204+
string setting = GetTypeAndSourceSetting("log", source, LogLevel.Warn.ToString());
205205
if (setting == null) {
206206
_minLogLevels.AddOrUpdate(source, LogLevel.Trace, (logName, level) => LogLevel.Trace);
207207
return LogLevel.Trace;

src/Platforms/Exceptionless.AspNetCore/Exceptionless.AspNetCore.csproj

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\..\..\build\common.props" />
33

4-
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
5-
<TargetFramework>netstandard2.0</TargetFramework>
6-
</PropertyGroup>
7-
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
8-
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
4+
<PropertyGroup>
5+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
96
</PropertyGroup>
107

118
<PropertyGroup Label="Package">
@@ -22,19 +19,15 @@
2219

2320
<ItemGroup Label="Project References">
2421
<ProjectReference Include="..\..\Exceptionless\Exceptionless.csproj" />
22+
<ProjectReference Include="..\Exceptionless.Extensions.Hosting\Exceptionless.Extensions.Hosting.csproj" />
2523
</ItemGroup>
2624

2725
<ItemGroup Label="Package References">
28-
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
29-
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
30-
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
31-
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
32-
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
33-
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.2.0" />
34-
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
26+
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />
27+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
28+
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.1.1" />
29+
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.1.1" />
3530
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.7.1" />
36-
<PackageReference Include="System.Net.Http" Version="4.3.4" />
37-
<PackageReference Include="System.Net.Primitives" Version="4.3.1" />
3831
</ItemGroup>
3932

4033
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' " Label="Build">

0 commit comments

Comments
 (0)