Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Serilog and SEQ applied in Feature/shahanaz/result processing/initial implemantion #507

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/Services/ResultProcessing/ResultProcessing.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,84 @@
namespace ResultProcessing.API
{
public class Program
Feature/Shahanaz/ResultProcessing/Initial-implemantion
{
public static readonly string Namespace = typeof(Program).Namespace!;
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Design",
"CA1031:Do not catch general exception types",
Justification = "Top level all exception catcher")]
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
Activity.DefaultIdFormat = ActivityIdFormat.W3C;

var configuration = GetConfiguration();

Log.Logger = CreateSerilogLogger(configuration);
try
{
Log.Information("Configuring web host ({ApplicationContext})...", AppName);
CreateHostBuilder(args).Build().Run();

return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(IConfiguration configuration,string[] args) =>

{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
main
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
Feature/Shahanaz/ResultProcessing/Initial-implemantion
webBuilder.UseConfiguration(configuration);
webBuilder.UseSerilog();
});

private static ILogger CreateSerilogLogger(IConfiguration configuration)
{
return new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.WithProperty("ApplicationContext", AppName)
.Enrich.FromLogContext()
.Enrich.WithSpan()
.WriteTo.Console()
.WriteTo.Seq("http://seq")
.ReadFrom.Configuration(configuration)
.CreateLogger();
}

private static IConfiguration GetConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();

return builder.Build();
}

});
main
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
Feature/Shahanaz/ResultProcessing/Initial-implemantion

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.11.0-dev-01367" />
<PackageReference Include="Serilog.Enrichers.Span" Version="1.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

</Project>

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.2"/>
</ItemGroup>
</Project>
</Project>
main
20 changes: 20 additions & 0 deletions src/Services/ResultProcessing/ResultProcessing.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ public void ConfigureServices(IServiceCollection services)
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Feature/Shahanaz/ResultProcessing/Initial-implemantion
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
var pathBase = Configuration["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase))
{
loggerFactory.CreateLogger<Startup>().LogInformation("Using PATH BASE '{pathBase}'", pathBase);
app.UsePathBase(pathBase);
}

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();

}
app.UseSerilogRequestLogging();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ExamManagement.API v1"));

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
Expand All @@ -45,6 +64,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
endpoints.MapControllers();
});
main
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
{
Feature/Shahanaz/ResultProcessing/Initial-implemantion
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"OpenCodeFoundation": "Information",
"System": "Warning"
}

"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
main
}
},
"AllowedHosts": "*"
Expand Down