Skip to content

Commit

Permalink
Monitoring and logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
okutbay committed Oct 22, 2024
1 parent d3a6726 commit 6898e82
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageVersion Include="BuildBundlerMinifier" Version="3.2.449" PrivateAssets="All" />
<PackageVersion Include="FluentValidation" Version="11.10.0" />
<PackageVersion Include="MediatR" Version="12.4.1" />
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Authorization" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.10" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public class CatalogItemListPagedEndpoint : IEndpoint<IResult, ListPagedCatalogI
{
private readonly IUriComposer _uriComposer;
private readonly IMapper _mapper;
private readonly IAppLogger<CatalogItemListPagedEndpoint> _logger;

public CatalogItemListPagedEndpoint(IUriComposer uriComposer, IMapper mapper)
public CatalogItemListPagedEndpoint(IUriComposer uriComposer, IMapper mapper, IAppLogger<CatalogItemListPagedEndpoint> logger)
{
_uriComposer = uriComposer;
_mapper = mapper;
_logger = logger;
}

public void AddRoute(IEndpointRouteBuilder app)
Expand All @@ -44,6 +46,9 @@ public async Task<IResult> HandleAsync(ListPagedCatalogItemRequest request, IRep

var filterSpec = new CatalogFilterSpecification(request.CatalogBrandId, request.CatalogTypeId);
int totalItems = await itemRepository.CountAsync(filterSpec);
_logger.LogInformation($"Total Items = {totalItems}");

// throw new Exception("Cannot move further: CatalogItemListPagedEndpoint HandleAsync");

var pagedSpec = new CatalogFilterPaginatedSpecification(
skip: request.PageIndex * request.PageSize,
Expand Down
17 changes: 17 additions & 0 deletions src/PublicApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.ApplicationInsights;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using MinimalApi.Endpoint.Configurations.Extensions;
using MinimalApi.Endpoint.Extensions;

var builder = WebApplication.CreateBuilder(args);

// The following line enables Application Insights telemetry collection.
builder.Services.AddApplicationInsightsTelemetry();

builder.Logging.AddApplicationInsights(
configureTelemetryConfiguration: (config) =>
config.ConnectionString = builder.Configuration.GetConnectionString("APPLICATIONINSIGHTS_CONNECTION_STRING"),
configureApplicationInsightsLoggerOptions: (options) => { }
);

builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("Filter", LogLevel.Trace);


builder.Services.AddEndpoints();

// Use to force loading of appsettings.json of test project
Expand Down Expand Up @@ -176,6 +189,10 @@
app.MapEndpoints();

app.Logger.LogInformation("LAUNCHING PublicApi");

// throw new Exception("Cannot move further: Program");

app.Run();


public partial class Program { }
3 changes: 2 additions & 1 deletion src/PublicApi/PublicApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Ardalis.ApiEndpoints" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" />
<PackageReference Include="MinimalApi.Endpoint" />
<PackageReference Include="Swashbuckle.AspNetCore" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" />
Expand All @@ -21,7 +22,7 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" >
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
14 changes: 10 additions & 4 deletions src/PublicApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning",
"System": "Warning"
"Default": "Information", // Warning
"Microsoft": "Information", // Warning
"System": "Information" // Warning
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ApplicationInsights": {
"ConnectionString": "Copy connection string from Application Insights Resource Overview",
"LogLevel": {
"Default": "Information"
}
}
}
}

0 comments on commit 6898e82

Please sign in to comment.