This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example-management): add seq and serilog (#491)
Co-authored-by: Ratan Sunder Parai <ratanparai@gmail.com>
- Loading branch information
1 parent
847fd24
commit 0cd1536
Showing
3 changed files
with
69 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,74 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using Serilog.Enrichers.Span; | ||
|
||
namespace ExamManagement.API | ||
{ | ||
public class Program | ||
{ | ||
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(string[] args) => | ||
public static IHostBuilder CreateHostBuilder(IConfiguration configuration,string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
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(); | ||
} | ||
} | ||
} |
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