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

Commit 0cd1536

Browse files
feat(example-management): add seq and serilog (#491)
Co-authored-by: Ratan Sunder Parai <ratanparai@gmail.com>
1 parent 847fd24 commit 0cd1536

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

src/Services/ExamManagement/ExamManagement.API/ExamManagement.API.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Serilog" Version="2.10.0" />
9+
<PackageReference Include="Serilog.Enrichers.Span" Version="1.3.0" />
10+
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
811
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
912
</ItemGroup>
1013

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,74 @@
11
using Microsoft.AspNetCore.Hosting;
22
using Microsoft.Extensions.Hosting;
3+
using Serilog;
4+
using Serilog.Enrichers.Span;
35

46
namespace ExamManagement.API
57
{
68
public class Program
79
{
10+
public static readonly string Namespace = typeof(Program).Namespace!;
11+
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
12+
13+
[System.Diagnostics.CodeAnalysis.SuppressMessage(
14+
"Design",
15+
"CA1031:Do not catch general exception types",
16+
Justification = "Top level all exception catcher")]
817
public static void Main(string[] args)
918
{
10-
CreateHostBuilder(args).Build().Run();
19+
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
20+
21+
var configuration = GetConfiguration();
22+
23+
Log.Logger = CreateSerilogLogger(configuration);
24+
try
25+
{
26+
Log.Information("Configuring web host ({ApplicationContext})...", AppName);
27+
CreateHostBuilder(args).Build().Run();
28+
29+
return 0;
30+
}
31+
catch (Exception ex)
32+
{
33+
Log.Fatal(ex, "Host terminated unexpectedly");
34+
return 1;
35+
}
36+
finally
37+
{
38+
Log.CloseAndFlush();
39+
}
1140
}
1241

13-
public static IHostBuilder CreateHostBuilder(string[] args) =>
42+
public static IHostBuilder CreateHostBuilder(IConfiguration configuration,string[] args) =>
1443
Host.CreateDefaultBuilder(args)
1544
.ConfigureWebHostDefaults(webBuilder =>
1645
{
1746
webBuilder.UseStartup<Startup>();
47+
webBuilder.UseConfiguration(configuration);
48+
webBuilder.UseSerilog();
1849
});
50+
51+
private static ILogger CreateSerilogLogger(IConfiguration configuration)
52+
{
53+
return new LoggerConfiguration()
54+
.MinimumLevel.Verbose()
55+
.Enrich.WithProperty("ApplicationContext", AppName)
56+
.Enrich.FromLogContext()
57+
.Enrich.WithSpan()
58+
.WriteTo.Console()
59+
.WriteTo.Seq("http://seq")
60+
.ReadFrom.Configuration(configuration)
61+
.CreateLogger();
62+
}
63+
64+
private static IConfiguration GetConfiguration()
65+
{
66+
var builder = new ConfigurationBuilder()
67+
.SetBasePath(Directory.GetCurrentDirectory())
68+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
69+
.AddEnvironmentVariables();
70+
71+
return builder.Build();
72+
}
1973
}
2074
}

src/Services/ExamManagement/ExamManagement.API/Startup.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Hosting;
66
using Microsoft.OpenApi.Models;
7+
using Serilog;
78

89
namespace ExamManagement.API
910
{
@@ -27,14 +28,20 @@ public void ConfigureServices(IServiceCollection services)
2728
});
2829
}
2930

30-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
31-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
31+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
3232
{
33+
var pathBase = Configuration["PATH_BASE"];
34+
if (!string.IsNullOrEmpty(pathBase))
35+
{
36+
loggerFactory.CreateLogger<Startup>().LogInformation("Using PATH BASE '{pathBase}'", pathBase);
37+
app.UsePathBase(pathBase);
38+
}
39+
3340
if (env.IsDevelopment())
3441
{
3542
app.UseDeveloperExceptionPage();
36-
3743
}
44+
app.UseSerilogRequestLogging();
3845
app.UseSwagger();
3946
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ExamManagement.API v1"));
4047

0 commit comments

Comments
 (0)