-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (70 loc) · 2.83 KB
/
Program.cs
File metadata and controls
81 lines (70 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;
namespace DotNetCoreReactREST
{
public class Program
{
public static void Main(string[] args)
{
// Add Serilog for logging through new configuration before the CreateHostBuilder function.
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
// Change the date format here
string date = DateTime.Now.ToString("ddMMyyyyHHmm");
// Change the filename here
string logfile = string.Concat("Logs/logfile_" + date + ".log");
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.Enrich.WithMachineName()
.WriteTo.Console(
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} UTC [{Level}] {Message}{NewLine}{Exception}")
.WriteTo.File(new CompactJsonFormatter(), logfile)
.CreateLogger();
Log.Information("Added logging to Program.cs before CreateHostBuilder so can log the try catch.");
try
{
Log.Information("Application starting up on {MachineName}");
// Create host
var host = CreateHostBuilder(args)
.Build();
// Run the web app
host.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application failed to even run.");
throw;
}
finally
{
// Make sure the log is closed correctly
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // <-- Add this line for serilog
.ConfigureWebHostDefaults(webBuilder =>
{
// Set defaults here
// If Environment is Production
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.Production.json")
.Build();
webBuilder.UseConfiguration(configuration);
}
webBuilder.UseStartup<Startup>();
});
}
}