Skip to content

Commit

Permalink
Add support for new style Asp.Net Core configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
twenzel committed Mar 7, 2018
1 parent 3f93ca3 commit 1e2c21b
Show file tree
Hide file tree
Showing 18 changed files with 249 additions and 160 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Current extensions:
Extension to the Microsoft.Extensions.Logging framework to demystify exceptions prior logging.

#### Usage
For ASP.NET Core applications simply call the `AddExceptionDemystifyer()` in `ConfigureServices`:
For ASP.NET Core applications (old style) simply call the `AddExceptionDemystifyer()` in `ConfigureServices`:

```csharp
public void ConfigureServices(IServiceCollection services)
Expand All @@ -25,6 +25,22 @@ public void ConfigureServices(IServiceCollection services)
}
```

For ASP.NET Core applications using the new style simply call the `AddExceptionDemystifyer()` in `ConfigureLogging`:

```csharp
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) =>
{
builder.AddConfiguration(context.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug()
.AddExceptionDemystifyer();
})
.UseStartup<Startup>()
.Build();
```

Or when using the logging framework elsewhere, just extend the LoggerFactory with `DemystifyExceptions()`:

```csharp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 2 additions & 9 deletions samples/ASPCoreDemystifyExceptions/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ public IActionResult Index()
return View();
}

public IActionResult About()
public IActionResult Test()
{
ViewData["Message"] = "Your application description page.";
ViewData["Message"] = "Test page";

new SampleExceptionGenerator();

return View();
}

public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";

return View();
}

public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
Expand Down
9 changes: 9 additions & 0 deletions samples/ASPCoreDemystifyExceptions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public static void Main(string[] args)

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) =>
{
builder
.AddConfiguration(context.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug()
.AddExceptionDemystifyer()
;
})
.UseStartup<Startup>()
.Build();
}
Expand Down
5 changes: 3 additions & 2 deletions samples/ASPCoreDemystifyExceptions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddExceptionDemystifyer();
// services.AddExceptionDemystifyer();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory factory)
{
factory.AddDebug();
//factory.AddDebug();
//factory.AddConsole();

if (env.IsDevelopment())
{
Expand Down
7 changes: 0 additions & 7 deletions samples/ASPCoreDemystifyExceptions/Views/Home/About.cshtml

This file was deleted.

17 changes: 0 additions & 17 deletions samples/ASPCoreDemystifyExceptions/Views/Home/Contact.cshtml

This file was deleted.

106 changes: 2 additions & 104 deletions samples/ASPCoreDemystifyExceptions/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,5 @@
ViewData["Title"] = "Home Page";
}

<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="6000">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="~/images/banner1.svg" alt="ASP.NET" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
Learn how to build ASP.NET apps that can run anywhere.
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525028&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
<div class="item">
<img src="~/images/banner2.svg" alt="Visual Studio" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
There are powerful new features in Visual Studio for building modern web apps.
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525030&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
<div class="item">
<img src="~/images/banner3.svg" alt="Package Management" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
Bring in libraries from NuGet, Bower, and npm, and automate tasks using Grunt or Gulp.
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525029&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
<div class="item">
<img src="~/images/banner4.svg" alt="Microsoft Azure" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
Learn how Microsoft's Azure cloud platform allows you to build, deploy, and scale web apps.
<a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkID=525027&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

<div class="row">
<div class="col-md-3">
<h2>Application uses</h2>
<ul>
<li>Sample pages using ASP.NET Core MVC</li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=518004">Bower</a> for managing client-side libraries</li>
<li>Theming using <a href="https://go.microsoft.com/fwlink/?LinkID=398939">Bootstrap</a></li>
</ul>
</div>
<div class="col-md-3">
<h2>How to</h2>
<ul>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=398600">Add a Controller and View</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=699315">Manage User Secrets using Secret Manager.</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=699316">Use logging to log a message.</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=699317">Add packages using NuGet.</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=699318">Add client packages using Bower.</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=699319">Target development, staging or production environment.</a></li>
</ul>
</div>
<div class="col-md-3">
<h2>Overview</h2>
<ul>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=518008">Conceptual overview of what is ASP.NET Core</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=699320">Fundamentals of ASP.NET Core such as Startup and middleware.</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=398602">Working with Data</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkId=398603">Security</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=699321">Client side development</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=699322">Develop on different platforms</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=699323">Read more on the documentation site</a></li>
</ul>
</div>
<div class="col-md-3">
<h2>Run &amp; Deploy</h2>
<ul>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=517851">Run your app</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=517853">Run tools such as EF migrations and more</a></li>
<li><a href="https://go.microsoft.com/fwlink/?LinkID=398609">Publish to Microsoft Azure Web Apps</a></li>
</ul>
</div>
</div>
<br />
<p>Please click on <a asp-action="Test">Test</a> to produce an exception.</p>
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Test">Test</a></li>
</ul>
</div>
</div>
</nav>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>&copy; 2017 - ASPCoreDemystifyExceptions</p>
</footer>
</div>

<environment include="Development">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ben.Demystifier" Version="0.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
<PackageReference Include="Ben.Demystifier" Version="0.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace codeessentials.Extensions.Logging.Demystifier
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;

namespace codeessentials.Extensions.Logging.Demystifier
{
public class LoggerProviderWrapper :ILoggerProvider
{
private readonly ILoggerProvider _inner;

public LoggerProviderWrapper(ILoggerProvider inner)
{
_inner = inner;
}

public ILogger CreateLogger(string categoryName)
{
return new LoggerWrapper(_inner.CreateLogger(categoryName));
}

public void Dispose()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using codeessentials.Extensions.Logging.Demystifier;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Logging
{
public static class LoggingBuilderExtensions
{
/// <summary>
/// Wraps the logger factory in order to demystify exceptions (using Ben.Demystifer) prior logging
/// </summary>
/// <param name="builder">The logging builder.</param>
/// <returns></returns>
public static ILoggingBuilder AddExceptionDemystifyer(this ILoggingBuilder builder)
{
builder.Services.AddExceptionDemystifyer();
builder.Services.Decorate<ILoggerProvider, LoggerProviderWrapper>();
return builder;
}
}
}
Loading

0 comments on commit 1e2c21b

Please sign in to comment.