-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new settings EnableContextLoggingMiddleware
- Loading branch information
support
committed
Nov 25, 2023
1 parent
197502e
commit 9a226fc
Showing
11 changed files
with
122 additions
and
31 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
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
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
29 changes: 29 additions & 0 deletions
29
src/Web/Grand.Web.Common/Middleware/ContextLoggingMiddleware.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,29 @@ | ||
using Grand.Infrastructure; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Grand.Web.Common.Middleware; | ||
|
||
public class ContextLoggingMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public ContextLoggingMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
var workContext = context.RequestServices.GetRequiredService<IWorkContext>(); | ||
|
||
var requestTelemetry = context.Features.Get<RequestTelemetry>(); | ||
requestTelemetry.Properties.TryAdd("Customer", workContext?.CurrentCustomer?.Email); | ||
requestTelemetry.Properties.TryAdd("Store", workContext?.CurrentStore?.Name); | ||
requestTelemetry.Properties.TryAdd("Currency", workContext?.WorkingCurrency?.Name); | ||
requestTelemetry.Properties.TryAdd("Language", workContext?.WorkingLanguage?.Name); | ||
|
||
await _next(context); | ||
} | ||
} |
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,52 @@ | ||
using Grand.Domain.Data; | ||
using Grand.Infrastructure; | ||
using Grand.Infrastructure.Configuration; | ||
using Grand.Web.Common.Middleware; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Grand.Web.Common.Startup | ||
{ | ||
/// <summary> | ||
/// Represents object for the configuring logger middleware on application startup | ||
/// </summary> | ||
public class LoggerStartup : IStartupApplication | ||
{ | ||
/// <summary> | ||
/// Add and configure any of the middleware | ||
/// </summary> | ||
/// <param name="services">Collection of service descriptors</param> | ||
/// <param name="configuration">Configuration root of the application</param> | ||
public void ConfigureServices(IServiceCollection services, IConfiguration configuration) | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Configure the using of added middleware | ||
/// </summary> | ||
/// <param name="application">Builder for configuring an application's request pipeline</param> | ||
/// <param name="webHostEnvironment">WebHostEnvironment</param> | ||
public void Configure(IApplicationBuilder application, IWebHostEnvironment webHostEnvironment) | ||
{ | ||
//check whether database is installed | ||
if (!DataSettingsManager.DatabaseIsInstalled()) | ||
return; | ||
|
||
var appConfig = application.ApplicationServices.GetRequiredService<AppConfig>(); | ||
|
||
//set context logging | ||
if(appConfig.EnableContextLoggingMiddleware) | ||
application.UseMiddleware<ContextLoggingMiddleware>(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets order of this startup configuration implementation | ||
/// </summary> | ||
public int Priority => 501; | ||
public bool BeforeConfigure => false; | ||
|
||
} | ||
} |
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