-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add better extension methods for Diagnostics Error Reporting.
- Make more of the old code obsolete. - Delete in XML docs samples that quickly get outdated. We have snippets.
- Loading branch information
1 parent
e1080c9
commit 037af1a
Showing
13 changed files
with
229 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../Google.Cloud.Diagnostics.AspNetCore/ErrorReporting/AspNetCoreErrorReportingExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Cloud.Diagnostics.Common; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
#if NETCOREAPP3_1 | ||
namespace Google.Cloud.Diagnostics.AspNetCore3 | ||
#elif NETSTANDARD2_0 | ||
namespace Google.Cloud.Diagnostics.AspNetCore | ||
#else | ||
#error unknown target framework | ||
#endif | ||
{ | ||
/// <summary> | ||
/// Extensions for configuring Google Cloud Error Reporting in ASP.NET Core applications. | ||
/// </summary> | ||
public static class AspNetCoreErrorReportingExtensions | ||
{ | ||
/// <summary> | ||
/// Configures Google Cloud Error Reporting for ASP .NET Core applications./>. | ||
/// </summary> | ||
public static IServiceCollection AddGoogleErrorReportingForAspNetCore( | ||
this IServiceCollection services, Common.ErrorReportingServiceOptions options = null) => | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
services.AddGoogleErrorReportingForAspNetCore(true, options); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
|
||
[Obsolete("Added for avoiding code duplication while we made old methods obsolete and included substitutes")] | ||
internal static IServiceCollection AddGoogleErrorReportingForAspNetCore( | ||
this IServiceCollection services, bool registerMiddleware, Common.ErrorReportingServiceOptions options) | ||
{ | ||
services.AddGoogleErrorReporting(options); | ||
services.AddHttpContextAccessor(); | ||
services.AddSingleton<IExceptionLogger>(sp => new GoogleExceptionLogger( | ||
sp.GetRequiredService<IContextExceptionLogger>(), | ||
sp.GetRequiredService<IHttpContextAccessor>())); | ||
|
||
if (registerMiddleware) | ||
{ | ||
// This registers the trace middleware so users don't have to. | ||
services.AddSingleton<IStartupFilter, AspNetCoreErrorReportingStartupFilter>(); | ||
} | ||
|
||
return services; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ogle.Cloud.Diagnostics.AspNetCore/ErrorReporting/AspNetCoreErrorReportingStartupFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using System; | ||
|
||
#if NETCOREAPP3_1 | ||
namespace Google.Cloud.Diagnostics.AspNetCore3 | ||
#elif NETSTANDARD2_0 | ||
namespace Google.Cloud.Diagnostics.AspNetCore | ||
#else | ||
#error unknown target framework | ||
#endif | ||
{ | ||
internal class AspNetCoreErrorReportingStartupFilter : IStartupFilter | ||
{ | ||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => | ||
app => next(app.UseMiddleware<ErrorReportingExceptionLoggerMiddleware>()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.