Read my blog here
Created
January 31, 2022 14:37
-
-
Save jstemerdink/7b3cf30d090cbcc2954d660150461007 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"Serilog": { | |
"Using": [ "Serilog.Sinks.File" ], | |
"MinimumLevel": { | |
"Default": "Information", | |
"Override": { | |
"Microsoft": "Warning", | |
"Microsoft.Hosting.Lifetime": "Information", | |
"EPiserver": "Information", | |
"EPiServer.Commerce": "Debug" | |
} | |
}, | |
"WriteTo": [ | |
{ | |
"Name": "File", | |
"Args": { | |
"path": "./logs/log-.txt", | |
"rollingInterval": "Day" | |
} | |
} | |
], | |
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], | |
"Destructure": [ | |
{ | |
"Name": "ToMaximumDepth", | |
"Args": { "maximumDestructuringDepth": 4 } | |
}, | |
{ | |
"Name": "ToMaximumStringLength", | |
"Args": { "maximumStringLength": 100 } | |
}, | |
{ | |
"Name": "ToMaximumCollectionCount", | |
"Args": { "maximumCollectionCount": 10 } | |
} | |
], | |
"Properties": { | |
"Application": "Core Commerce" | |
} | |
} | |
// Other configuration | |
} |
This file contains hidden or 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
using System; | |
using System.IO; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Hosting; | |
using Serilog; | |
public class Program | |
{ | |
public static IConfiguration Configuration { get; } = | |
new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appSettings.json", false, true) | |
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", true) | |
.AddEnvironmentVariables() | |
.Build(); | |
public static IHostBuilder CreateHostBuilder(string[] args, bool isDevelopment) | |
{ | |
if (isDevelopment) | |
{ | |
return Host.CreateDefaultBuilder(args: args) | |
.ConfigureCmsDefaults() | |
.UseSerilog() | |
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); | |
} | |
return Host.CreateDefaultBuilder(args: args) | |
.ConfigureCmsDefaults() | |
.UseSerilog() | |
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); | |
} | |
public static int Main(string[] args) | |
{ | |
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | |
bool isDevelopment = environment == Environments.Development; | |
if (isDevelopment) | |
{ | |
// Development configuration | |
} | |
Log.Logger = new LoggerConfiguration() | |
.ReadFrom.Configuration(configuration: Configuration) | |
.CreateLogger(); | |
Log.Information("Starting up!"); | |
Log.Information("Logging enabled"); | |
try | |
{ | |
CreateHostBuilder(args: args, isDevelopment: isDevelopment).Build().Run(); | |
Log.Information("Stopped cleanly"); | |
return 0; | |
} | |
catch (Exception ex) | |
{ | |
Log.Fatal(exception: ex, "An unhandled exception occured during bootstrapping"); | |
return 1; | |
} | |
finally | |
{ | |
Log.CloseAndFlush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment