Skip to content

Commit

Permalink
hotfix serilog, dropped autofac
Browse files Browse the repository at this point in the history
  • Loading branch information
md-redwan-hossain committed Dec 20, 2023
1 parent 6314578 commit aa063f8
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 86 deletions.
8 changes: 6 additions & 2 deletions BdGeographicalData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0"/>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.8.1"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0"/>
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0"/>
Expand All @@ -20,5 +18,11 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Content Include="app.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
29 changes: 18 additions & 11 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using BdGeographicalData.Api.District;
using BdGeographicalData.Api.Division;
using BdGeographicalData.Api.SubDistrict;
using BdGeographicalData.Shared;
using BdGeographicalData.Utils;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
Expand All @@ -16,18 +17,26 @@
.WriteTo.Async(wt => wt.Console())
.CreateBootstrapLogger();


try
{
var builder = WebApplication.CreateBuilder(args);


builder.Configuration.AddJsonFile(Constants.SecretsJsonFileName, optional: true, reloadOnChange: true);

builder.Host.UseSerilog((_, loggerConfiguration) => loggerConfiguration
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Enrich.FromLogContext()
.ReadFrom.Configuration(builder.Configuration)
.WriteTo.Async(x => x.Console(
restrictedToMinimumLevel: LogEventLevel.Information,
outputTemplate: Constants.SerilogConsoleOutputTemplate
))
.WriteTo.Async(x => x.AsyncRollingFile(
restrictedToMinimumLevel: LogEventLevel.Error,
pathFormat: Constants.SerilogFilePath
))
);

builder.Services.AddOptions<AppOptions>()
Expand All @@ -42,15 +51,13 @@
ResponseCacheDurationInSecond = section.GetValue<int>("ResponseCacheDurationInSecond")
};


builder.Services.AddDbContext<BdGeographicalDataDbContext>(opts =>
opts.UseSqlite(options.DatabaseUrl));

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder =>
{
containerBuilder.RegisterModule(new ApiModule());
});
builder.Services.AddScoped<ResponseCacheConfigMiddleware>();
builder.Services.AddScoped<IDivisionService, DivisionService>();
builder.Services.AddScoped<IDistrictService, DistrictService>();
builder.Services.AddScoped<ISubDistrictService, SubDistrictService>();


builder.Services
Expand Down Expand Up @@ -100,7 +107,7 @@

catch (Exception e)
{
Console.WriteLine(e.GetType());
Console.WriteLine(e);
Log.Fatal(e, "Failed to start application");
return 1;
}
Expand Down
31 changes: 0 additions & 31 deletions Shared/ApiModule.cs

This file was deleted.

5 changes: 5 additions & 0 deletions Shared/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public readonly record struct Constants
public const string PageQueryKey = "page";
public const string LimitQueryKey = "limit";
public const string SortingQueryKey = "sort_order";

public const string SerilogFilePath = "Logs/web-log.log";

public const string SerilogConsoleOutputTemplate =
"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}";
}
1 change: 0 additions & 1 deletion Shared/ResponseCacheConfigMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public Task InvokeAsync(HttpContext context, RequestDelegate next)
var responseCachingFeature = context.Features.Get<IResponseCachingFeature>();
if (responseCachingFeature is not null)
responseCachingFeature.VaryByQueryKeys = new[] { "*" };

return next(context);
}
}
7 changes: 0 additions & 7 deletions appsettings.Development.json

This file was deleted.

7 changes: 0 additions & 7 deletions appsettings.Production.json

This file was deleted.

32 changes: 5 additions & 27 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
{
"Serilog": {
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"restrictedToMinimumLevel": "Information",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"restrictedToMinimumLevel": "Warning",
"path": "Logs/web-log-.log",
"rollingInterval": "Day"
}
}
]
}
}
]
"AllowedHosts": "*",
"AppOptions": {
"ResponseCacheDurationInSecond": 600,
"DatabaseUrl": "DataSource=app.db;Cache=Shared"
}
}
}

0 comments on commit aa063f8

Please sign in to comment.